Debian Codegraph @

texk/web2c/otps/routines.c

raw
1 /* routines.c: Generating the finite state automaton.
2
3 This file is part of Omega,
4 which is based on the web2c distribution of TeX.
5
6 Copyright (c) 1994--2001 John Plaice and Yannis Haralambous
7 Copyright (C) 2005, 2006 Roozbeh Pournader
8
9 Omega is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 Omega is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Omega; if not, write to the Free Software Foundation, Inc.,
21 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22
23 */
24
25 #include <kpathsea/config.h>
26 #include <kpathsea/types.h>
27 #include <kpathsea/c-std.h>
28 #include "routines.h"
29 #include "otp.h"
30
31 #define out_ptr (states[cur_state].length)
32 #define out_array (states[cur_state].instrs)
33
34 list left_false_holes;
35
36 int left_state;
37 int right_offset;
38 int input_bytes;
39 int output_bytes;
40
41 int no_states = 0;
42 int cur_state = 0;
43 int room_for_states = 0;
44 state_type states[OTP_MAXSTATES];
45
46 int no_tables = 0;
47 int cur_table = 0;
48 int room_for_tables = 0;
49 table_type tables[OTP_MAXTABLES];
50
51 int no_aliases = 0;
52 alias_pair aliases[OTP_MAXALIASES];
53
54 /* cons = [x] + L */
55 list
56 cons(int x, list L)
57 {
58 list temp;
59 temp = (list) malloc(sizeof(cell));
60 temp->val = x;
61 temp->ptr = L;
62 return temp;
63 }
64
65 /* list1 = [x] */
66 list
67 list1(int x)
68 {
69 list temp;
70 temp = (list) malloc(sizeof(cell));
71 temp->val = x;
72 temp->ptr = nil;
73 return temp;
74 }
75
76 /* list2 = [x, y] */
77 list
78 list2(int x, int y)
79 {
80 list temp, temp1;
81 temp = (list) malloc(sizeof(cell));
82 temp1 = (list) malloc(sizeof(cell));
83 temp->val = x;
84 temp->ptr = temp1;
85 temp1->val = y;
86 temp1->ptr = nil;
87 return temp;
88 }
89
90 /* append = copy(K) + L */
91 list
92 append(list K, list L)
93 {
94 if (K==nil) return L;
95 return cons(K->val, append(K->ptr, L));
96 }
97
98 /* append1 = copy(L) + [x] */
99 list
100 append1(list L, int x)
101 {
102 return (append(L,list1(x)));
103 }
104
105 /* lcons = [x] + L */
106 llist
107 lcons(left x, llist L)
108 {
109 llist temp;
110 temp = (llist) malloc(sizeof(lcell));
111 temp->val = x;
112 temp->ptr = L;
113 return temp;
114 }
115
116 /* llist1 = [x] */
117 llist
118 llist1(left x)
119 {
120 llist temp;
121 temp = (llist) malloc(sizeof(lcell));
122 temp->val = x;
123 temp->ptr = nil;
124 return temp;
125 }
126
127 /* llist2 = [x, y] */
128 llist
129 llist2(left x, left y)
130 {
131 llist temp, temp1;
132 temp = (llist) malloc(sizeof(lcell));
133 temp1 = (llist) malloc(sizeof(lcell));
134 temp->val = x;
135 temp->ptr = temp1;
136 temp1->val = y;
137 temp1->ptr = nil;
138 return temp;
139 }
140
141 /* lappend = copy(K) + L */
142 llist
143 lappend(llist K, llist L)
144 {
145 if (K==nil) return L;
146 return lcons(K->val, lappend(K->ptr, L));
147 }
148
149 /* lappend1 = copy(L) + [x] */
150 llist
151 lappend1(llist L, left x)
152 {
153 return (lappend(L,llist1(x)));
154 }
155
156 left
157 WildCard(void)
158 {
159 left temp;
160 temp = (left) malloc(sizeof(lft_cell));
161 temp->kind=WILDCARD;
162 return temp;
163 }
164
165 left
166 StringLeft(char *x)
167 {
168 left temp;
169 temp = (left) malloc(sizeof(lft_cell));
170 temp->kind=STRINGLEFT;
171 temp->valstr=x;
172 return temp;
173 }
174
175 left
176 SingleLeft(int x)
177 {
178 left temp;
179 temp = (left) malloc(sizeof(lft_cell));
180 temp->kind=SINGLELEFT;
181 temp->val1=x;
182 return temp;
183 }
184
185 left
186 DoubleLeft(int x, int y)
187 {
188 left temp;
189 temp = (left) malloc(sizeof(lft_cell));
190 temp->kind=DOUBLELEFT;
191 temp->val1=x;
192 temp->val2=y;
193 return temp;
194 }
195
196 left
197 ChoiceLeft(llist L)
198 {
199 left temp;
200 temp = (left) malloc(sizeof(lft_cell));
201 temp->kind=CHOICELEFT;
202 temp->more_lefts=L;
203 return temp;
204 }
205
206 left
207 NotChoiceLeft(llist L)
208 {
209 left temp;
210 temp = (left) malloc(sizeof(lft_cell));
211 temp->kind=NOTCHOICELEFT;
212 temp->more_lefts=L;
213 return temp;
214 }
215
216 left
217 PlusLeft(left l, int n)
218 {
219 left temp;
220 if (n == 0) { FATAL ("plusleft's argument must be non-zero"); }
221 temp = (left) malloc(sizeof(lft_cell));
222 temp->kind=PLUSLEFT;
223 temp->one_left=l;
224 temp->val1=n;
225 return temp;
226 }
227
228 left
229 CompleteLeft(left l, int n, int m)
230 {
231 left temp;
232 if (n == 0) { FATAL ("completeleft's first argument must be non-zero"); }
233 temp = (left) malloc(sizeof(lft_cell));
234 temp->kind=COMPLETELEFT;
235 temp->one_left=l;
236 temp->val1=n;
237 temp->val2=m;
238 return temp;
239 }
240
241 left
242 BeginningLeft(void)
243 {
244 left temp;
245 temp = (left) malloc(sizeof(lft_cell));
246 temp->kind=BEGINNINGLEFT;
247 return temp;
248 }
249
250 left
251 EndLeft(void)
252 {
253 left temp;
254 temp = (left) malloc(sizeof(lft_cell));
255 temp->kind=ENDLEFT;
256 return temp;
257 }
258
259 list
260 gen_left(left arg)
261 {
262 int save_ptr, k;
263 list holes, false_holes, true_holes, backup_holes;
264 char *runner;
265 llist p;
266
267 switch(arg->kind) {
268 case WILDCARD:
269 return nil;
270 case STRINGLEFT:
271 runner=arg->valstr;
272 holes=nil;
273 while (*runner) {
274 out_int(OTP_GOTO_NE, *runner);
275 out_int(0, 0);
276 holes=cons(out_ptr-1,holes);
277 runner++;
278 if (*runner) {
279 out_int(OTP_GOTO_NO_ADVANCE, 0);
280 holes = cons(out_ptr-1, holes);
281 }
282 }
283 return holes;
284 case SINGLELEFT:
285 out_int(OTP_GOTO_NE, arg->val1);
286 out_int(0, 0);
287 return list1(out_ptr-1);
288 case DOUBLELEFT:
289 holes = nil;
290 if (arg->val1 > 0) {
291 out_int(OTP_GOTO_LT, arg->val1);
292 out_int(0, 0);
293 holes = list1(out_ptr-1);
294 }
295 if (arg->val2 < 0xFFFF) {
296 out_int(OTP_GOTO_GT, arg->val2);
297 out_int(0, 0);
298 holes = cons(out_ptr-1, holes);
299 }
300 return holes;
301 case CHOICELEFT:
302 true_holes=nil;
303 false_holes=nil;
304 p=arg->more_lefts;
305 while (p!=nil) {
306 false_holes = gen_left(p->val);
307 if (p->ptr) {
308 out_int(OTP_GOTO, 0);
309 true_holes=cons(out_ptr-1, true_holes);
310 fill_in(false_holes);
311 }
312 p=p->ptr;
313 }
314 fill_in(true_holes);
315 return false_holes;
316 case NOTCHOICELEFT:
317 true_holes=nil;
318 p=arg->more_lefts;
319 while (p!=nil) {
320 false_holes = gen_left(p->val);
321 out_int(OTP_GOTO, 0);
322 true_holes=cons(out_ptr-1, true_holes);
323 fill_in(false_holes);
324 p=p->ptr;
325 }
326 return true_holes;
327 case PLUSLEFT:
328 false_holes=nil;
329 true_holes=nil;
330 backup_holes=nil;
331 k=1;
332 while (k<arg->val1) {
333 holes = gen_left(arg->one_left);
334 false_holes = append(false_holes, holes);
335 out_int(OTP_GOTO_NO_ADVANCE, 0);
336 false_holes = cons(out_ptr-1, false_holes);
337 k++;
338 }
339 holes=gen_left(arg->one_left);
340 false_holes = append(false_holes, holes);
341 save_ptr = out_ptr;
342 out_int(OTP_GOTO_NO_ADVANCE, 0);
343 true_holes=cons(out_ptr-1, true_holes);
344 backup_holes=gen_left(arg->one_left);
345 out_int(OTP_GOTO, save_ptr);
346 fill_in(backup_holes);
347 out_int(OTP_LEFT_BACKUP, 0);
348 fill_in(true_holes);
349 return false_holes;
350 case COMPLETELEFT:
351 false_holes=nil;
352 true_holes=nil;
353 backup_holes=nil;
354 k=1;
355 if (arg->val1 > arg->val2) {
356 return nil;
357 }
358 while (k<=arg->val1) {
359 holes = gen_left(arg->one_left);
360 false_holes = append(false_holes, holes);
361 out_int(OTP_GOTO_NO_ADVANCE, 0);
362 false_holes = cons(out_ptr-1, false_holes);
363 k++;
364 }
365 while (k<arg->val2) {
366 holes = gen_left(arg->one_left);
367 true_holes = append(true_holes, holes);
368 out_int(OTP_GOTO_NO_ADVANCE, 0);
369 backup_holes = cons(out_ptr-1, backup_holes);
370 k++;
371 }
372 holes = gen_left(arg->one_left);
373 true_holes = append(true_holes, holes);
374 out_int(OTP_GOTO, out_ptr+2);
375 fill_in(true_holes);
376 out_int(OTP_LEFT_BACKUP, 0);
377 fill_in(backup_holes);
378 return false_holes;
379 case BEGINNINGLEFT:
380 out_int(OTP_GOTO_BEG, 0);
381 true_holes=list1(out_ptr-1);
382 out_int(OTP_GOTO, 0);
383 false_holes=list1(out_ptr-1);
384 fill_in(true_holes);
385 return false_holes;
386 case ENDLEFT:
387 out_int(OTP_GOTO_END, 0);
388 true_holes=list1(out_ptr-1);
389 out_int(OTP_GOTO, 0);
390 false_holes=list1(out_ptr-1);
391 fill_in(true_holes);
392 return false_holes;
393 default:
394 FATAL1 ("unrecognized left: %d", arg->kind);
395 }
396 }
397
398 void
399 store_alias(string str, left l)
400 {
401 int i;
402 for (i=0; i<no_aliases; i++) {
403 if (!strcmp(str,aliases[i].str)) {
404 FATAL1 ("alias %s already defined", str);
405 }
406 }
407 aliases[i].str=xstrdup(str);
408 aliases[i].left_val=l;
409 no_aliases++;
410 }
411
412 left
413 lookup_alias(string str)
414 {
415 int i;
416 for (i=0; i<no_aliases; i++) {
417 if (!strcmp(str,aliases[i].str)) {
418 return aliases[i].left_val;
419 }
420 }
421 FATAL1 ("alias %s not defined", str);
422 }
423
424 void
425 out_left(llist L)
426 {
427 llist p;
428 list holes;
429 if ((states[cur_state].no_exprs)==1) {
430 out_int(OTP_LEFT_START, 0);
431 } else {
432 out_int(OTP_LEFT_RETURN, 0);
433 }
434 p=L;
435 left_false_holes=nil;
436 while (p!=nil) {
437 holes = gen_left(p->val);
438 if ((p->ptr != nil) &&
439 ((p->val)->kind !=BEGINNINGLEFT) &&
440 (((p->ptr)->val)->kind !=ENDLEFT)) {
441 out_int(OTP_GOTO_NO_ADVANCE, 0);
442 left_false_holes = cons(out_ptr-1,left_false_holes);
443 }
444 left_false_holes = append(left_false_holes, holes);
445 p=p->ptr;
446 }
447 }
448
449 void
450 fill_in_left(void)
451 {
452 out_int(OTP_STOP, 0);
453 fill_in(left_false_holes);
454 }
455
456 void
457 fill_in(list L)
458 {
459 list p;
460 p=L;
461 while (p!=0) {
462 out_array[p->val] = out_array[p->val] + out_ptr;
463 p=p->ptr;
464 }
465 }
466
467 void
468 out_right(int instr, int val)
469 {
470 out_int(instr+right_offset, val);
471 }
472
473 void
474 out_int(int instr, int val)
475 {
476 if (val>=(1<<24)) {
477 FATAL1 ("Argument (%d) of instruction too big", val);
478 }
479 add_to_state((instr<<24)+val);
480 }
481
482 void
483 store_state(const_string str)
484 {
485 int i;
486 for (i=0; i<no_states; i++) {
487 if (!strcmp(str,states[i].str)) {
488 FATAL1 ("state %s already defined", str);
489 }
490 }
491 states[i].str=xstrdup(str);
492 states[i].length=0;
493 states[i].no_exprs=0;
494 cur_state=i;
495 no_states++;
496 }
497
498 int
499 lookup_state(string str)
500 {
501 int i;
502 for (i=0; i<no_states; i++) {
503 if (!strcmp(str,states[i].str)) {
504 return i;
505 }
506 }
507 FATAL1 ("state %s not defined", str);
508 }
509
510 void
511 add_to_state(int x)
512 {
513 int len;
514 len = states[cur_state].length;
515 if (len > ARRAY_SIZE) {
516 char * str = states[cur_state].str;
517 FATAL2 ("%s state has too many instructions: %d", str, len);
518 }
519 (states[cur_state].instrs)[len] = x;
520 states[cur_state].length = len+1;
521 }
522
523 void
524 store_table(string str, int len)
525 {
526 int i;
527 for (i=0; i<no_tables; i++) {
528 if (!strcmp(str,tables[i].str)) {
529 FATAL1 ("table %s already defined", str);
530 }
531 }
532 tables[i].str=xstrdup(str);
533 tables[i].length=0;
534 cur_table=i;
535 no_tables++;
536 }
537
538 void
539 add_to_table(int x)
540 {
541 int len;
542 len = tables[cur_table].length;
543 (tables[cur_table].table)[len] = x;
544 tables[cur_table].length = len+1;
545 }
546
547 int
548 lookup_table(string str)
549 {
550 int i;
551 for (i=0; i<no_tables; i++) {
552 if (!strcmp(str,tables[i].str)) {
553 return i;
554 }
555 }
556 FATAL1 ("table %s not defined", str);
557 }
558