r3tex

custom LuaTeX format
git clone git://git.rr3.xyz/r3tex
Log | Files | Refs | README | LICENSE

commit 68b823326e4fc3d32c9f1083b1227484f2175cd6
parent cab34ebad92b5cca64cbf93a01ce4f42cbf499f6
Author: robert <robertrussell.72001@gmail.com>
Date:   Tue,  7 Sep 2021 19:07:08 -0700

Clean up

Diffstat:
Malloc.tex | 57++++++++++++++++++++++++++++++++++-----------------------
Ddefer.tex | 26--------------------------
Mr3tex.tex | 7+++++--
Mstack.tex | 53++++++++++++++++++++++++++++-------------------------
Mutil.tex | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
5 files changed, 126 insertions(+), 92 deletions(-)

diff --git a/alloc.tex b/alloc.tex @@ -1,5 +1,7 @@ -% Resources are allocated as follows. By "temporary", we mean the register -% may be clobbered by any macro. All ranges are inclusive. +% Resources are allocated as follows. All ranges are inclusive. +% By "temporary", we mean the register may be clobbered by any macro. In some +% cases, not being able to use temporary registers with certain macros is very +% restrictive, so these macros are documented as not using temporary registers. % count: % 0--9: page identification % 10--29: internal to allocator @@ -34,6 +36,7 @@ % 256--32767: allocated % TODO: marks? How do eTeX marks classes work? +\countdef\allocnum=10 \allocnum=-1 \countdef\countnext=10 \countnext=256 \chardef\countmax=65535 \countdef\dimennext=11 \dimennext=256 \chardef\dimenmax=65535 \countdef\skipnext=12 \skipnext=256 \chardef\skipmax=65535 @@ -51,25 +54,26 @@ \ifnum#3>#4% \errmessage{No room for a new \string#1}% \else - \global#2#5=#3% - \wlog{\string#5=\string#1\the#3}% + \allocnum=#3% + \global#2#5=\allocnum + \wlog{\string#5=\string#1\the\allocnum}% \global\advance#31 \fi } -\def\newcount{\allocate\count\countdef\countnext\countmax} -\def\newdimen{\allocate\dimen\dimendef\dimennext\dimenmax} -\def\newskip{\allocate\skip\skipdef\skipnext\skipmax} -\def\newmuskip{\allocate\muskip\muskipdef\muskipnext\muskipmax} -\def\newbox{\allocate\box\chardef\boxnext\boxmax} -\def\newtoks{\allocate\toks\toksdef\toksnext\toksmax} -\def\newread{\allocate\read\chardef\readnext\readmax} -\def\newwrite{\allocate\write\chardef\writenext\writemax} -\def\newfam{\allocate\fam\chardef\famnext\fammax} -\def\newattr{\allocate\attr\chardef\attrnext\attrmax} -\def\newcctab{\allocate\cctab\chardef\cctabnext\cctabmax} +\protected\def\newcount{\allocate\count\countdef\countnext\countmax} +\protected\def\newdimen{\allocate\dimen\dimendef\dimennext\dimenmax} +\protected\def\newskip{\allocate\skip\skipdef\skipnext\skipmax} +\protected\def\newmuskip{\allocate\muskip\muskipdef\muskipnext\muskipmax} +\protected\def\newbox{\allocate\box\chardef\boxnext\boxmax} +\protected\def\newtoks{\allocate\toks\toksdef\toksnext\toksmax} +\protected\def\newread{\allocate\read\chardef\readnext\readmax} +\protected\def\newwrite{\allocate\write\chardef\writenext\writemax} +\protected\def\newfam{\allocate\fam\chardef\famnext\fammax} +\protected\def\newattr{\allocate\attr\chardef\attrnext\attrmax} +\protected\def\newcctab{\allocate\cctab\chardef\cctabnext\cctabmax} -\def\newinsert#1{% +\protected\def\newinsert#1{% \ifnum\insertnext<\insertmin \errmessage{No room for a new \string\insert}% \else @@ -79,6 +83,8 @@ \fi } +% TODO do these belong here? +\chardef\maxchar=1114111 \newcount\maxcount \maxcount="7FFFFFFF \newdimen\maxdimen \maxdimen=16383.99999pt \newskip\hideskip \hideskip=-1000pt plus1fill @@ -92,18 +98,23 @@ \muskipdef\muskipA=30 \muskipdef\muskipB=31 \muskipdef\muskipC=32 \chardef\boxA=30 \chardef\boxB=31 \chardef\boxC=32 \toksdef\toksA=30 \toksdef\toksB=31 \toksdef\toksC=32 -\undef\macA \undef\macB \undef\macC +% temporary control sequences: csA csB csC -\def\newif#1{% - \countA=\escapechar \escapechar=-1 - \csdef{\ea\@remif\string#1true}{\let#1=\iftrue}% - \csdef{\ea\@remif\string#1false}{\let#1=\iffalse}% +% \protected\def\newif#1{% + % \countA=\escapechar \escapechar=-1 + % \csdef{\ea\@remif\string#1true}{\let#1=\iftrue}% + % \csdef{\ea\@remif\string#1false}{\let#1=\iffalse}% + % \let#1=\iffalse + % \escapechar=\countA +% } +\protected\def\newif#1{% + \csdef{\ea\@remif\csstring#1true}{\let#1=\iftrue}% + \csdef{\ea\@remif\csstring#1false}{\let#1=\iffalse}% \let#1=\iffalse - \escapechar=\countA } {\uccode`1=`i \uccode`2=`f \uppercase{\gdef\@remif12{}}} -% TODO: do we really need this? +% TODO: do we really need this? If so, protect % Allocation of unique control sequences. Example usage: % \mycount=\setuniq\mymacro % Save \mymacro % ... diff --git a/defer.tex b/defer.tex @@ -1,26 +0,0 @@ -% \defer defers execution of the given tokens until after the current group. -% \edefer is the same as \defer, except it expands the given tokens first (in -% the same way \edef does). -% Note: a defer can't contain more than 1 "child" defer. -% E.g., \defer{A\defer{B\defer{C}}} is ok, but not \defer{A\defer{B}\defer{C}}. -\newcount\defernext \defernext=0 -\def\defer#1#2{% - \ea\gdef\csname defer:\the\defernext\endcsname{#2}% - \ea\aftergroup\csname defer:\the\defernext\endcsname - \advance\defernext -} - -% \deferasn defers execution of the given tokens until after the next -% assignment. For box register assignments, the tokens are placed after the -% entire assignment, unlike with \afterassignment. \deferasn does not use any -% temporary registers. -\newcount\@grouplevel -\def\deferasn#1{% - \@grouplevel=\currentgrouplevel - \def\@deferasnB{#1}% - \def\@deferasnA{% - \ifnum\currentgrouplevel>\@grouplevel \ea\aftergroup \fi - \@deferasnB - }% - \afterassignment\@deferasnA -} diff --git a/r3tex.tex b/r3tex.tex @@ -1,8 +1,7 @@ \input catcode % plain-like catcode initialization \input primitive % LuaTeX primitives, pdfTeX compatibility -\input util \input alloc -\input defer % TODO merge into util +\input util \input stack % stack data structures \input loop \input debug % utilities for debugging @@ -26,6 +25,10 @@ \def\fmtname{r3TeX} \def\fmtversion{0.1} +\normalbaselines +\nonfrenchspacing +% TODO margins, font preload + % TODO: add author info, etc. with \pdfinfo % TODO: \author, \title, \date, etc. should parse to eol diff --git a/stack.tex b/stack.tex @@ -5,17 +5,20 @@ \newmuskip\pmuskip \newbox\pbox \newtoks\ptoks +\let\@stacktokn=\countA -\def\newstack#1{\newbox#1\global\setbox#1=\hbox{\penalty0}} +% TODO make expandable with immediateassignment? + +\protected\def\newstack#1{\newbox#1\global\setbox#1=\hbox{\penalty0}} \def\@stackop#1{% \global\setbox\@stackn=\hbox{% \unhbox\@stackn - \countA=\lastpenalty \unpenalty + \@stacktokn=\lastpenalty \unpenalty #1% - \penalty\countA + \penalty\@stacktokn }% } -\def\@stackcs{stack:\the\@stackn:\the\countA} +\def\@stackcs{stack:\the\@stackn:\the\@stacktokn} \def\@push#1#2{\deferasn{\deferasn{\@stackop{#2}}\global#1}\@stackn=} \def\@pop#1{\deferasn{\@stackop{#1}}\@stackn=} \def\isstackempty{% @@ -25,28 +28,28 @@ }\@stackn=% } -\def\pushcount{\@push\pcount{\penalty\pcount}} -\def\pushdimen{\@push\pdimen{\kern\pdimen}} -\def\pushskip{\@push\pskip{\hskip\pskip}} -\def\pushmuskip{\@push{\pskip\mutoglue}{\hskip\pskip}} -\def\pushbox{\@push{\setbox\pbox}{\box\pbox}} -\def\pushtoks{\@push\ptoks{\xcsdef\@stackcs{\the\ptoks}\incr\countA}} -\def\pushtok{\@push{\let\ptok}{\gcslet\@stackcs\ptok\incr\countA}} +\protected\def\pushcount{\@push\pcount{\penalty\pcount}} +\protected\def\pushdimen{\@push\pdimen{\kern\pdimen}} +\protected\def\pushskip{\@push\pskip{\hskip\pskip}} +\protected\def\pushmuskip{\@push{\pskip\mutoglue}{\hskip\pskip}} +\protected\def\pushbox{\@push{\setbox\pbox}{\box\pbox}} +\protected\def\pushtoks{\@push\ptoks{\xcsdef\@stackcs{\the\ptoks}\incr\@stacktokn}} +\protected\def\pushtok{\@push{\let\ptok}{\gcslet\@stackcs\ptok\incr\@stacktokn}} -\def\popcount{\@pop{\global\pcount\lastpenalty \unpenalty}} -\def\popdimen{\@pop{\global\pdimen\lastkern \unkern}} -\def\popskip{\@pop{\global\pskip\lastskip \unskip}} -\def\popmuskip{\@pop{\global\pmuskip\gluetomu\lastskip \unskip}} -\def\popbox{\@pop{\global\setbox\pbox\lastbox}} -\def\poptoks{\@pop{\decr\countA \gletcs\ptok\@stackcs \global\ptoks\ea{\ptok}}} -\def\poptok{\@pop{\decr\countA \gletcs\ptok\@stackcs}} +\protected\def\popcount{\@pop{\global\pcount\lastpenalty \unpenalty}} +\protected\def\popdimen{\@pop{\global\pdimen\lastkern \unkern}} +\protected\def\popskip{\@pop{\global\pskip\lastskip \unskip}} +\protected\def\popmuskip{\@pop{\global\pmuskip\gluetomu\lastskip \unskip}} +\protected\def\popbox{\@pop{\global\setbox\pbox\lastbox}} +\protected\def\poptoks{\@pop{\decr\@stacktokn \gletcs\ptok\@stackcs \global\ptoks\ea{\ptok}}} +\protected\def\poptok{\@pop{\decr\@stacktokn \gletcs\ptok\@stackcs}} -\def\peekcount{\@pop{\global\pcount\lastpenalty}} -\def\peekdimen{\@pop{\global\pdimen\lastkern}} -\def\peekskip{\@pop{\global\pskip\lastskip}} -\def\peekmuskip{\@pop{\global\pmuskip\gluetomu\lastskip}} -\def\peekbox{\@pop{\global\setbox\pbox\lastbox \copy\pbox}} -\def\peektoks{\@pop{{\decr\countA \gletcs\ptok\@stackcs}\global\ptoks\ea{\ptok}}} -\def\peektok{\@pop{{\decr\countA \gletcs\ptok\@stackcs}}} +\protected\def\peekcount{\@pop{\global\pcount\lastpenalty}} +\protected\def\peekdimen{\@pop{\global\pdimen\lastkern}} +\protected\def\peekskip{\@pop{\global\pskip\lastskip}} +\protected\def\peekmuskip{\@pop{\global\pmuskip\gluetomu\lastskip}} +\protected\def\peekbox{\@pop{\global\setbox\pbox\lastbox \copy\pbox}} +\protected\def\peektoks{\@pop{{\decr\@stacktokn \gletcs\ptok\@stackcs}\global\ptoks\ea{\ptok}}} +\protected\def\peektok{\@pop{{\decr\@stacktokn \gletcs\ptok\@stackcs}}} \endinput diff --git a/util.tex b/util.tex @@ -1,35 +1,44 @@ \let\bgroup={ \let\egroup=} +% Primitive abbreviations. \let\ea=\expandafter \let\attr=\attribute \let\cctab=\catcodetable \let\initcctab=\initcatcodetable \let\savecctab=\savecatcodetable +\let\begcs=\csname +\let\endcs=\endcsname +\let\ifcs=\ifcsname + +\def\nea{\noexpand\expandafter} % Useful inside \edef's. \def\wlog{\immediate\write-1 } \def\wterm{\immediate\write16 } -% Caution: \incr\count0 does not do what you'd hope. -\protected\def\incr#1{\advance#11 } +% Caution: \incr\count0 does not do what you'd hope. We use the "by" keyword +% to catch this error. +\protected\def\incr#1{\advance#1by1 } \protected\def\gincr{\global\incr} -\protected\def\decr#1{\advance#1-1 } +\protected\def\decr#1{\advance#1by-1 } \protected\def\gdecr{\global\decr} -\protected\def\csdef#1{\ea\def\csname#1\endcsname} -\protected\def\ecsdef#1{\ea\edef\csname#1\endcsname} -\protected\def\gcsdef#1{\ea\gdef\csname#1\endcsname} -\protected\def\xcsdef#1{\ea\xdef\csname#1\endcsname} +\def\cs#1{\begcs#1\endcs} + +\protected\def\csdef#1{\ea\def\begcs#1\endcs} +\protected\def\ecsdef#1{\ea\edef\begcs#1\endcs} +\protected\def\gcsdef#1{\ea\gdef\begcs#1\endcs} +\protected\def\xcsdef#1{\ea\xdef\begcs#1\endcs} \protected\def\undef#1{\let#1\@undefined} -\ifprimitive\glet \else % TODO: update LuaTeX. +\ifprimitive\glet \else \protected\def\glet{\global\let} \fi -\protected\def\cslet#1{\ea\let\csname#1\endcsname} -\protected\def\gcslet#1{\ea\glet\csname#1\endcsname} -\protected\def\letcs#1#2{\ea\let\ea#1\csname#2\endcsname} -\protected\def\gletcs#1#2{\ea\glet\ea#1\csname#2\endcsname} -\protected\def\csletcs#1#2{\ea\let\csname#1\ea\endcsname\csname#2\endcsname} -\protected\def\gcsletcs#1#2{\ea\glet\csname#1\ea\endcsname\csname#2\endcsname} +\protected\def\cslet#1{\ea\let\begcs#1\endcs} +\protected\def\gcslet#1{\ea\glet\begcs#1\endcs} +\protected\def\letcs#1#2{\ea\let\ea#1\begcs#2\endcs} +\protected\def\gletcs#1#2{\ea\glet\ea#1\begcs#2\endcs} +\protected\def\csletcs#1#2{\ea\let\begcs#1\ea\endcs\begcs#2\endcs} +\protected\def\gcsletcs#1#2{\ea\glet\begcs#1\ea\endcs\begcs#2\endcs} \def\selectx#1{} % \gobble \def\selectxx#1#2{} % \gobbletwo @@ -44,8 +53,8 @@ \def\space{ } \def\null{\hbox{}} -{\catcode`\p=12 \catcode`\t=12 \gdef\macA#1pt{#1}} -\let\rempt=\macA +{\catcode`\p=12 \catcode`\t=12 \gdef\csA#1pt{#1}} +\let\rempt=\csA \def\decimal{\ea\rempt\the} \chardef\%=`\% @@ -53,4 +62,38 @@ \chardef\#=`\# \chardef\$=`\$ +\def\@regnum#1#2\@END{% + \ifx\relax#1\else + \ifnum1<1#1 #1\fi + \@regnum#2\@END + \fi +} +\def\regnum#1{\ea\@regnum\meaning#1\relax\@END\relax} + +% \defer defers execution of the given tokens until after the current group +% (like \aftergroup, but with multiple tokens). Note: a defer can not contain +% more than 1 "child" defer. E.g., \defer{A\defer{B\defer{C}}} is ok, but not +% \defer{A\defer{B}\defer{C}}. +\newcount\defernext \defernext=0 +\def\defer#1{% + \gcsdef{defer:\the\defernext}{#1}% + \ea\aftergroup\begcs defer:\the\defernext\endcs + \advance\defernext +} + +% \deferasn defers execution of the given tokens until after the next +% assignment (like \afterassignment, but with multiple tokens). For box +% register assignments, the tokens are placed after the entire assignment, +% unlike with \afterassignment. \deferasn does not use any temporary registers. +\newcount\@grouplevel +\def\deferasn#1{% + \@grouplevel=\currentgrouplevel + \def\@deferasnB{#1}% + \def\@deferasnA{% + \ifnum\currentgrouplevel>\@grouplevel \ea\aftergroup \fi + \@deferasnB + }% + \afterassignment\@deferasnA +} + \endinput