summaryrefslogtreecommitdiff
path: root/support/splint/tex/yyfaststack.sty
blob: 9bf46b3d1e64fc2eb83bd6a857428e6ce3d82f6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
% Copyright 2012-2020, Alexander Shibakov
% This file is part of SPLinT
%
% SPLinT is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% SPLinT is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with SPLinT.  If not, see <http://www.gnu.org/licenses/>.

% fast(er) versions of stack access functions

\catcode`\@=11

\def\yyinitstack#1{% #1 is the name of the stack
    \csname \expandafter\eatone\string#1<count>\endcsname\m@ne
}

% stacks will be defined as pairs ( \s t a c k n a m e [n u m b e r], \s t a c k n a m e <count> )
% where \s t a c k n a m e [n u m b e r] is an associative array of control sequences
% and \s t a c k n a m e <count> is a \count register
%
% note that in the implementaion below the stack grows `the wrong
% way'; this is done for convenience, since there is no `memory read
% with increment' operation, the `usual' (say, hardware) stack
% implementations take advantage of, and our stack can grow
% unrestricted this way; also note that while the array is namespace specific,
% the stack pointer is not, so special effort is required to preserve it

\let\yyinitarray\yyinitstack % using the stack as an array only makes sense for the optimized version

\def\getstackpointer#1{% expands to the value of the current top of the stack position
    \expandafter\the\csname \expandafter\eatone\string#1<count>\endcsname
}

\def\gettopofstackcs#1{% expands to the control sequence (a member of the associative array)
                       % that holds the value of the current element at the top of the stack
    \csname \expandafter\eatone\string#1[\getstackpointer#1]\parsernamespace \endcsname
}

\def\gettopofstackcsx#1{% a version of the above to be used with \romannumeral
    0\expandafter\noexpand\csname \expandafter\eatone\string#1[\getstackpointer#1]\parsernamespace \endcsname
}

\def\gettopofstackcsxx#1{% to be used with \romannumeral, expands to the contents of the top stack element
    0\expandafter\expandafter\expandafter\space\csname \expandafter\eatone\string#1[\getstackpointer#1]\parsernamespace \endcsname
}

\def\getmidstackcs#1#2{% expands to the control sequence (a member of the associative array)
                       % that holds the value of the current element at the specified (#2) position
    \csname \expandafter\eatone\string#1[#2]\parsernamespace\endcsname
}

\def\getmidstackcsx#1#2{% a version of the above to be used with \romannumeral, safe to use with \edef
    0\expandafter\noexpand\csname \expandafter\eatone\string#1[#2]\parsernamespace\endcsname
}

\def\movestackpointer#1\by#2{% 
    \expandafter\advance\csname \expandafter\eatone\string#1<count>\endcsname#2%
}

\def\yypopstack#1\by#2{% removes the top #2 elements from the top of the stack #1
    \movestackpointer#1\by{-#2}\relax
}

%

\def\yypop#1\into#2{% pops the stack #1, stores the top element in token register #2
    #2\expandafter{\romannumeral\gettopofstackcsxx#1}%
    \movestackpointer#1\by\m@ne
}

% pushing stuff on a stack: \yypush{t o k e n s}\on\yyvs or \expandafter\yypush\number\yystate\on\yyss

\long\def\yypush#1\on#2{%
    \movestackpointer#2\by\@ne
    \expandafter\expandafter\expandafter\def\gettopofstackcs#2{#1}%
}

\long\def\yypushx#1\on#2{% push with expand
    \movestackpointer#2\by\@ne
    \expandafter\expandafter\expandafter\edef\gettopofstackcs#2{#1}%
}

% push register contents on a stack: #1 is a register, #2 is a stack

\def\yypushr#1\on#2{%
    \movestackpointer#2\by\@ne
    \expandafter\expandafter\expandafter\edef\gettopofstackcs#2{\the#1}%
}

% the first parameter is the stack, the second is the location from the top (a nonnegative number), the third is
% the control sequence that will hold the value;

\def\yyreadstack#1\at#2\to#3{%
    \expandafter\advance\csname \expandafter\eatone\string#1<count>\endcsname-#2\relax
    %
    \expandafter\let\expandafter#3\csname \expandafter\eatone\string#1[\expandafter\the
        \csname \expandafter\eatone\string#1<count>\endcsname]\parsernamespace\endcsname
    %
    \expandafter\advance\csname \expandafter\eatone\string#1<count>\endcsname#2\relax
}

% same as above but reads the value into a register

\def\yyreadstackr#1\at#2\to#3{%
    \expandafter\advance\csname \expandafter\eatone\string#1<count>\endcsname-#2\relax
    %
    #3\csname \expandafter\eatone\string#1[\expandafter\the
        \csname \expandafter\eatone\string#1<count>\endcsname]\parsernamespace\endcsname\relax
    %
    \expandafter\advance\csname \expandafter\eatone\string#1<count>\endcsname#2\relax
}

% the macros below use general purpose registers and have a chance to interfere with
% the user namespace; they have been replaced by the macros that only use expandable
% arithmetic which leads to about 3 percent slowdown. thus the original macros were left 
% in the package in case such a slowdown is undesirable

\def\yyreadvstack#1\upto#2{% assume that #2 > 0
    \tempca=#2\relax
    \tempcb\toptoks
    \advance\tempcb\@ne
    \yyreadvst@ck{#1}%
}

\def\yyreadvst@ck#1{%
    \expandafter\toksdef\csname$$'\the\tempca\endcsname=\tempcb%
    \expandafter\yypop\expandafter#1\expandafter\into\csname$$'\the\tempca\endcsname%$
    \expandafter\expandafter\expandafter\def
        \expandafter\expandafter\csname$'\the\tempca\endcsname\expandafter{\the\toks\tempcb}%$
    \ifnum\tempca=\@ne %we have read the values
        \yybreak\relax
    \else
        \advance\tempcb\@ne
        \advance\tempca\m@ne
        \yybreak{\yyreadvst@ck{#1}}%
    \yycontinue
}

% the macros below replace the two macros above to only use expandable arithmetic

\def\yyreadvstack#1\upto#2{% assume that #2 > 0
    \expandafter\yyr@@dvst@ck\expandafter{\number#2}{#1}%
}

\def\yyr@@dvst@ck#1#2{%
    \expandafter\yyr@@@vst@@k\expandafter{\number\toptoks}{#1}{#2}%
}

\def\yyr@@@vst@@k#1#2#3{%
    \yyr@@dvst@@k{#2}{#1}{#3}%
}

\def\yyreadvst@ck#1#2#3{%
    \expandafter\toksdef\csname$$'#2\endcsname=#1\relax %
    \expandafter\yypop\expandafter#3\expandafter\into\csname$$'#2\endcsname%$
    % the portion below is redundant if the \yn macro below is used and no symbolic switch
    % is implemented
    \expandafter\expandafter\expandafter\def
        \expandafter\expandafter\csname$'#2\endcsname\expandafter{\the\toks#1}%$
    \ifnum#2=\@ne %we have read the values
        \yybreak\relax
    \else
        \yybreak{\expandafter\yyr@@dvst@@k\expandafter{\number\xdecrement{#2}}{#1}{#3}}%
    \yycontinue
}

\def\yyr@@dvst@@k#1#2#3{%
    \expandafter\yyreadvst@ck\expandafter{\number\xincrement{#2}}{#1}{#3}%
}

% end of replacement macros

\def\yypeekvstack#1\upto#2{% assume that #2 > 0
    \yyreadvstack#1\upto{#2}\relax
    \movestackpointer#1\by#2\relax
}

% macros for reading the state stack (needed if using bison version 3.0 and above)

\def\yyreadsstack#1\upto#2\withprefix#3{% assume that #2 > 0
    \expandafter\yyr@@dsst@ck\expandafter{\number#2}{#1}{#3}%
}

\def\yyr@@dsst@ck#1#2#3{%
    \expandafter\yyr@@@sst@@k\expandafter{\number\toptoks}{#1}{#2}{#3}%
}

\def\yyr@@@sst@@k#1#2#3#4{%
    \yyr@@dsst@@k{#2}{#1}{#3}{#4}{}%
}

\def\yyr@@dsst@@k#1#2#3#4#5{%
    \expandafter\yyreadsst@ck\expandafter{\number\xincrement{#2}}{#1}{#3}{#4}{#5}%
}

\def\yyreadsst@ck#1#2#3#4#5{%
    \ifnum#2=\z@ %we have read the values
        \yybreak{#5}%
    \else
        \yybreak{\expandafter\yyr@@dsst@@@\expandafter{\number\gettopofstackcs#3}{#1}{#2}{#3}{#4}{#5}}%
    \yycontinue
}

\def\yyr@@dsst@@@#1#2#3#4#5#6{%
    \movestackpointer#4\by\m@ne
    \expandafter\yyr@@dsst@@k\expandafter{\number\xdecrement{#3}}{#2}{#4}{#5}{#5{#1}#6}%
}

\def\yypeeksstack#1\upto#2\withprefix#3{% assume that #2 > 0
    \yyreadsstack#1\upto{#2}\withprefix{#3}\relax
    \movestackpointer#1\by#2\relax
}

% faster array access macros

\def\fastgetelemof#1\at#2{%
    \csname #1\parsernamespace\number#2\endcsname
}

\def\fgetelemof#1\at#2{% this definition allows mixing optimized and unoptimized tables
    \expandafter\ifx\csname optopt[#1]\parsernamespace\endcsname\relax
        \getelemof{#1}\at{#2}%
    \else
        \csname #1\parsernamespace\number#2\endcsname
    \fi
}

% new stack access macros that read the stack directly (to reduce namespace pollution)
% these are here more as an example

\def\yn#1#{%
  \ifnum#1<\@ne
      \yybreak{\putyyval}%
  \else
      \yybreak{\p@tyyassignment{#1}}%
  \yycontinue
}

\def\p@tyyassignment#1{%
    \expandafter\advance\csname yyvsa<count>\endcsname-\yylen
    \expandafter\advance\csname yyvsa<count>\endcsname#1\relax
    %
    \expandafter\expandafter\expandafter
        \p@@yyassignment
        \expandafter\expandafter\expandafter{%
            \csname yyvsa[\expandafter\the
                \csname yyvsa<count>\endcsname]\parsernamespace\endcsname
        }{%
            \expandafter\advance\csname yyvsa<count>\endcsname-#1\relax
            \expandafter\advance\csname yyvsa<count>\endcsname\yylen
        }%
}

% \bb macro implementation

\def\p@twwassignment#1{%
    \expandafter\advance\csname yyvsa<count>\endcsname-#1\relax
    \expandafter\advance\csname yyvsa<count>\endcsname\@ne
    %
    \expandafter\expandafter\expandafter
        \p@@yyassignment
        \expandafter\expandafter\expandafter{%
            \csname yyvsa[\expandafter\the
                \csname yyvsa<count>\endcsname]\parsernamespace\endcsname
        }{%
            \expandafter\advance\csname yyvsa<count>\endcsname#1\relax
            \expandafter\advance\csname yyvsa<count>\endcsname\m@ne
        }%
}

\def\p@@yyassignment#1#2#3{%
    #2\yystringempty{#3}{#1}{#3{#1}}%
}

\def\putyyval#1{\edef\next{\yyval{#1}}\next}

% dysplaying stack contents (\yyparse specific)

\def\showstack#1{%
    \begingroup
        \let\stackcs\empty
        \count\z@\csname \expandafter\eatone\string#1<count>\endcsname
        \ifnum\count\z@<\z@
        \else
            \bloop
                \count\@ne\csname \expandafter\eatone\string#1[\the\count\z@]\parsernamespace\endcsname
                \count\tw@=\fgetelemof{yystos}\at{\count\@ne}%
                \edef\stackcs{\stackcs^^J \space\space\fgetelemof{yytname}\at{\count\tw@}}%
            \ifnum\count\z@>\z@
                \advance\count\z@\m@ne
            \repeat
        \fi
    \tokreturn{}{}{\def\noexpand\stackcs{\stackcs}}%
}

% saving token registers

\def\newtable@generic#1{%
    \expandafter\ifx\csname #1\endcsname\relax
        \toksa{\csname newtoks\endcsname}%
        \expandafter\the\expandafter\toksa\csname #1\endcsname
    \fi
    \csname #1\endcsname=%
}

\let\newtable\newtable@generic % TODO: right now this is done either for all tables or none of them

% macros to support `hardwired' optimized tables

\def\appendtoyy@generic#1{%
    \toksa{\or}%
    \expandafter\ifx\csname optopt[#1]\parsernamespace\endcsname\relax
        \expandafter\concat\csname#1\endcsname\toksb
        \expandafter\concat\csname#1\endcsname\toksa
    \else
        \expandafter\edef\csname #1\parsernamespace\the\tempca\endcsname{\the\toksb}%
    \fi
}

\def\appendtoyy@genericlast#1{%
    \expandafter\ifx\csname optopt[#1]\parsernamespace\endcsname\relax
        \expandafter\concat\csname#1\endcsname\toksb
    \else
        \expandafter\edef\csname #1\parsernamespace\the\tempca\endcsname{\the\toksb}%
    \fi
}

\def\beginoptimizednonnumeric#1{%
    \tempca\z@
    \def\appendtoyytname{\appendtoyy@generic{#1}}%
    \def\appendtoyytnamelast{\appendtoyy@genericlast{#1}}%
}

% list macros

\def\listx#1{% #1 is the list name
    \expandafter\ifx\csname #1[\the\csname#1<count>\endcsname]\endcsname\relax
        \csname#1<count>\endcsname\z@
        \yybreak\relax
    \else
        \yybreak{\csname #1[\the\csname#1<count>\endcsname]\endcsname
            \expandafter\advance\csname#1<count>\endcsname\@ne
            \listx{#1}}%
    \yycontinue
}

\def\newlist#1{%
    \toksa{\csname newcount\endcsname}%
    \expandafter\the\expandafter\toksa\csname#1<count>\endcsname
}

\def\initlistx#1{%
    \csname#1<count>\endcsname\z@
    \expandafter\let\csname #10\endcsname\relax
}

\def\resetlistx#1{%
    \csname#1<count>\endcsname\z@
}

\def\listxlastindex#1{%
    \the\csname#1<count>\endcsname
}

\def\addtolist#1#2{%
    \expandafter\def\csname#1[\the\csname#1<count>\endcsname]\endcsname{#2}%
    \expandafter\advance\csname#1<count>\endcsname\@ne
}

\def\addxtolist#1#2{%
    \expandafter\edef\csname#1[\the\csname#1<count>\endcsname]\endcsname{#2}%
    \expandafter\advance\csname#1<count>\endcsname\@ne
}

\def\finishlist#1{%
    \expandafter\advance\csname#1<count>\endcsname\@ne
    \expandafter\let\csname#1[\the\csname#1<count>\endcsname]\endcsname\relax
}