| 1 |
\input ptb-utils |
| 2 |
\_checkloaded{stacks} |
| 3 |
|
| 4 |
% Creates a macro stack, a stack which stores the definition of macros. |
| 5 |
% #1: The name of the stack. |
| 6 |
\def\createmacrostack#1{\_xp\def\csname mstack@#1@sp\endcsname{0}} |
| 7 |
|
| 8 |
% Gets the current pointer on a macro stack. |
| 9 |
% #1: The name of the stack. |
| 10 |
\def\macrostackpointer#1{\csname mstack@#1@sp\endcsname} |
| 11 |
|
| 12 |
% Pushes a macro onto a macro stack. |
| 13 |
% #1: The name of the stack. |
| 14 |
% #2: The control sequence to push onto the stack. |
| 15 |
\def\macrostackpush#1#2{% |
| 16 |
\_xp\edef\csname mstack@#1@sp\endcsname{\the\numexpr \csname mstack@#1@sp\endcsname + 1\relax}% |
| 17 |
\_xp\let\csname mstack@#1@def@\csname mstack@#1@sp\endcsname\endcsname=#2% |
| 18 |
\_xp\edef\csname mstack@#1@name@\csname mstack@#1@sp\endcsname\endcsname{\_mstrip#2}% |
| 19 |
} |
| 20 |
|
| 21 |
% Gets a macro on a stack. |
| 22 |
% #1: The name of the stack |
| 23 |
% #2: The index on the stack |
| 24 |
\def\macrostackget#1#2{% |
| 25 |
\csname mstack@#1@def@#2\endcsname% |
| 26 |
} |
| 27 |
|
| 28 |
% Decrements the stack pointer of a macrostack without restoring the definition of the control sequence. |
| 29 |
% #1: The name of the stack. |
| 30 |
\def\macrostackdecrement#1{% |
| 31 |
\_xp\let\csname mstack@#1@name@\csname mstack@#1@sp\endcsname\endcsname=\undefined% |
| 32 |
\_xp\let\csname mstack@#1@def@\csname mstack@#1@sp\endcsname\endcsname=\undefined% |
| 33 |
\_xp\edef\csname mstack@#1@sp\endcsname{\the\numexpr \csname mstack@#1@sp\endcsname - 1\relax}% |
| 34 |
} |
| 35 |
|
| 36 |
% Sets the control sequence from the top of the stack, without popping it. |
| 37 |
% #1: The name of the stack |
| 38 |
\def\macrostackset#1{% |
| 39 |
\_xplet{\csname\csname mstack@#1@name@\csname mstack@#1@sp\endcsname\endcsname\endcsname}{\csname mstack@#1@def@\csname mstack@#1@sp\endcsname\endcsname}% |
| 40 |
} |
| 41 |
|
| 42 |
% Pops a control sequence off of the top of the stack. |
| 43 |
% This restores the definition of the control sequence. |
| 44 |
% #1: The name of the stack. |
| 45 |
\def\macrostackpop#1{% |
| 46 |
\macrostackset{#1}% |
| 47 |
\macrostackdecrement{#1}% |
| 48 |
} |
| 49 |
|
| 50 |
% Peeks at the top of a macro stack. |
| 51 |
% #1: The name of the stack. |
| 52 |
% #2: A control sequence to put the definition of the control sequence at the top of the stack into. |
| 53 |
% #3: A control sequence to put the name of the control sequence at the top of the stack into. |
| 54 |
\def\macrostackpeek#1#2#3{% |
| 55 |
\_xp\let#2\csname mstack@#1@def@\csname mstack@#1@sp\endcsname\endcsname% |
| 56 |
\_xp\let#3\csname mstack@#1@name@\csname mstack@#1@sp\endcsname\endcsname% |
| 57 |
} |
| 58 |
|
| 59 |
\def\createstack#1{\_xp\def\csname stack@#1@sp\endcsname{0}} |
| 60 |
|
| 61 |
\def\stackpush#1#2{% |
| 62 |
\_xp\edef\csname stack@#1@sp\endcsname{\the\numexpr \csname stack@#1@sp\endcsname + 1\relax}% |
| 63 |
\_xp\def\csname stack@#1@\csname stack@#1@sp\endcsname\endcsname{#2}% |
| 64 |
} |
| 65 |
|
| 66 |
\def\stackdecrement#1{% |
| 67 |
\_xp\let\csname stack@#1@\csname stack@#1@sp\endcsname\endcsname=\undefined% |
| 68 |
\_xp\edef\csname stack@#1@sp\endcsname{\the\numexpr \csname stack@#1@sp\endcsname - 1\relax}% |
| 69 |
} |
| 70 |
|
| 71 |
\def\stackpop#1#2{% |
| 72 |
\stacktop{#1}{#2}% |
| 73 |
\stackdecrement{#1}% |
| 74 |
} |
| 75 |
|
| 76 |
\def\stacktop#1#2{% |
| 77 |
\_xp\let\_xp#2\csname stack@#1@\csname stack@#1@sp\endcsname\endcsname% |
| 78 |
} |
| 79 |
|
| 80 |
\def\_rstackpush#1#2{\stackpush{#2}{#1}} |
| 81 |
\def\_rstackpop#1#2{\stackpop{#2}{#1}} |
| 82 |
|
| 83 |
\createmacrostack{macro_stack} |
| 84 |
\createstack{macro_stack_ptr} |
| 85 |
|
| 86 |
\def\macro_stack_ptr{0} |
| 87 |
\def\beginscope{% |
| 88 |
\_xp\_rstackpush\_xp{\macro_stack_ptr}{macro_stack_ptr}% |
| 89 |
} |
| 90 |
|
| 91 |
\def\endscope{% |
| 92 |
\stackpop{macro_stack_ptr}\_uregB% |
| 93 |
\loop\ifnum\macro_stack_ptr>\_uregB% |
| 94 |
\macrostackpop{macro_stack}% |
| 95 |
\edef\macro_stack_ptr{\the\numexpr \macro_stack_ptr - 1\relax}% |
| 96 |
\repeat% |
| 97 |
} |
| 98 |
|
| 99 |
\def\localize#1{% |
| 100 |
\macrostackpush{macro_stack}{#1}% |
| 101 |
\edef\macro_stack_ptr{\the\numexpr \macro_stack_ptr + 1\relax}% |
| 102 |
} |
| 103 |
|
| 104 |
|