comparison tests/agcl/parsifal/good/sort.c @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 class String {
2 char *value;
3 public:
4 String();
5 String(unsigned char *);
6 int operator < (const String &);
7 };
8
9 /*
10 * AnaGram, A System for Syntax Directed Programming
11 * File generated by: ...
12 *
13 * AnaGram Parsing Engine
14 * Copyright 1993-2002 Parsifal Software. All Rights Reserved.
15 *
16 * This software is provided 'as-is', without any express or implied
17 * warranty. In no event will the authors be held liable for any damages
18 * arising from the use of this software.
19 *
20 * Permission is granted to anyone to use this software for any purpose,
21 * including commercial applications, and to alter it and redistribute it
22 * freely, subject to the following restrictions:
23 *
24 * 1. The origin of this software must not be misrepresented; you must not
25 * claim that you wrote the original software. If you use this software
26 * in a product, an acknowledgment in the product documentation would be
27 * appreciated but is not required.
28 * 2. Altered source versions must be plainly marked as such, and must not be
29 * misrepresented as being the original software.
30 * 3. This notice may not be removed or altered from any source distribution.
31 */
32
33 #ifndef SORT_H
34 #include "sort.h"
35 #endif
36
37 #ifndef SORT_H
38 #error Mismatched header file
39 #endif
40
41 #include <ctype.h>
42 #include <stdio.h>
43
44 #define RULE_CONTEXT (&((PCB).cs[(PCB).ssx]))
45 #define ERROR_CONTEXT ((PCB).cs[(PCB).error_frame_ssx])
46 #define CONTEXT ((PCB).cs[(PCB).ssx])
47
48
49
50 sort_pcb_type sort_pcb;
51 #define PCB sort_pcb
52
53 /* Line -, sort.syn */
54 #include <string.h>
55 #include <stdio.h>
56 #include "agmap.h"
57
58
59 #define GET_CONTEXT CONTEXT = PCB.pointer
60
61 AgMap<String, String> map;
62
63 String::String() : value(new char[PCB.pointer - CONTEXT + 1]) {
64 int length = PCB.pointer - CONTEXT;
65 strncpy(value, CONTEXT, length);
66 value[length] = 0;
67 }
68
69 String::String(unsigned char *p) : value(new char[p - CONTEXT + 1]) {
70 int length = p - CONTEXT;
71 strncpy(value, CONTEXT, length);
72 value[length] = 0;
73 }
74
75 int operator < (const String &s) {
76 return strcmp(value, s.value) < 0;
77 }
78
79 enum ConditionCode {
80 success,
81 incorrectArgumentCount,
82 cannotOpenInput,
83 cannotSeek,
84 ftellFailure,
85 insufficientMemory,
86 readError,
87 parseError
88 };
89
90 int main(int argc, char *argv[]) {
91
92 /* Check for enough arguments */
93 if (argc != 2) {
94 printf("Usage: %s <filename>\n", argv[0]);
95 return incorrectArgumentCount;
96 }
97
98 /* Open input file */
99 FILE *input = fopen(argv[1],"r");
100 if (input == NULL) {
101 printf("Cannot open %s\n", argv[1]);
102 return cannotOpenInput;
103 }
104
105 /* find out how big the file is */
106 if (fseek(input, SEEK_SET, SEEK_END)) {
107 printf("Seek error on %s\n", argv[1]);
108 return cannotSeek;
109 }
110 long fileLength = ftell(input);
111 if (fileLength < 0 ) { // -1L is error return
112 printf("Error getting file length (%d) of %s\n", fileLength, argv[1]);
113 return ftellFailure;
114 }
115 /* fseek to beginning of file */
116 if (fseek(input, 0, SEEK_SET)) {
117 printf("Seek error on %s\n", argv[1]);
118 return cannotSeek;
119 }
120
121 /* Allocate storage for input string */
122 char *scriptString = new char[(size_t) fileLength + 1];
123 if (scriptString == NULL) {
124 printf("Insufficient memory\n");
125 return insufficientMemory;
126 }
127
128 /* read file */
129 size_t stringLength = fread(scriptString, 1, (unsigned)fileLength, input);
130 if (stringLength == 0) {
131 printf("Unable to read %s\n", argv[1]);
132 delete [] scriptString;
133 fclose(input);
134 return readError;
135 }
136 scriptString[stringLength] = 0; // Terminate string with null
137 fclose(input);
138
139 PCB.pointer = scriptString;
140 sort();
141
142
143
144 return success;
145 }
146
147
148 #ifndef CONVERT_CASE
149 #define CONVERT_CASE(c) (c)
150 #endif
151 #ifndef TAB_SPACING
152 #define TAB_SPACING 8
153 #endif
154
155 #define ag_rp_1() (PCB.pointer)
156
157 #define ag_rp_3(p) (String(p))
158
159 #define ag_rp_4(w) (w)
160
161 #define ag_rp_5(t) (map[t] = t)
162
163 #define ag_rp_6(a) (map[a] = String())
164
165 #define ag_rp_7(a) (map[a] = String())
166
167
168 #define READ_COUNTS
169 #define WRITE_COUNTS
170 #undef V
171 #define V(i,t) (*t (&(PCB).vs[(PCB).ssx + i]))
172 #undef VS
173 #define VS(i) (PCB).vs[(PCB).ssx + i]
174
175 #ifndef GET_CONTEXT
176 #define GET_CONTEXT CONTEXT = (PCB).input_context
177 #endif
178
179 typedef enum {
180 ag_action_1,
181 ag_action_2,
182 ag_action_3,
183 ag_action_4,
184 ag_action_5,
185 ag_action_6,
186 ag_action_7,
187 ag_action_8,
188 ag_action_9,
189 ag_action_10,
190 ag_action_11,
191 ag_action_12
192 } ag_parser_action;
193
194
195 #ifndef NULL_VALUE_INITIALIZER
196 #define NULL_VALUE_INITIALIZER = { 0 }
197 #endif
198
199 static sort_vs_type const ag_null_value NULL_VALUE_INITIALIZER;
200
201 static const unsigned char ag_rpx[] = {
202 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 2, 3, 4, 5, 6
203 };
204 #define AG_TCV(x) (((int)(x) >= 0 && (int)(x) <= 255) ? ag_tcv[(x)] : 0)
205
206 static const unsigned char ag_tcv[] = {
207 16, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0,
208 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 5, 5,
209 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
210 5, 5, 5, 5, 5, 5, 10, 5, 11, 5, 5, 5, 5, 5, 5, 5, 5, 5,
211 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
212 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
213 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
214 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
216 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
218 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
219 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
220 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
221 0, 0, 0, 0
222 };
223
224 #ifndef SYNTAX_ERROR
225 #define SYNTAX_ERROR fprintf(stderr,"%s, line %d, column %d\n", \
226 (PCB).error_message, (PCB).line, (PCB).column)
227 #endif
228
229 #ifndef FIRST_LINE
230 #define FIRST_LINE 1
231 #endif
232
233 #ifndef FIRST_COLUMN
234 #define FIRST_COLUMN 1
235 #endif
236
237 #ifndef PARSER_STACK_OVERFLOW
238 #define PARSER_STACK_OVERFLOW {fprintf(stderr, \
239 "\nParser stack overflow, line %d, column %d\n",\
240 (PCB).line, (PCB).column);}
241 #endif
242
243 #ifndef REDUCTION_TOKEN_ERROR
244 #define REDUCTION_TOKEN_ERROR {fprintf(stderr, \
245 "\nReduction token error, line %d, column %d\n", \
246 (PCB).line, (PCB).column);}
247 #endif
248
249
250 #ifndef GET_INPUT
251 #define GET_INPUT ((PCB).input_code = getchar())
252 #endif
253
254
255 #ifndef AG_NEWLINE
256 #define AG_NEWLINE 10
257 #endif
258
259 #ifndef AG_RETURN
260 #define AG_RETURN 13
261 #endif
262
263 #ifndef AG_FORMFEED
264 #define AG_FORMFEED 12
265 #endif
266
267 #ifndef AG_TABCHAR
268 #define AG_TABCHAR 9
269 #endif
270
271 static void ag_track(void) {
272 switch ((PCB).input_code) {
273 case AG_NEWLINE:
274 (PCB).column = 1, (PCB).line++;
275 case AG_RETURN:
276 case AG_FORMFEED:
277 break;
278 case AG_TABCHAR:
279 (PCB).column += (TAB_SPACING) - ((PCB).column - 1) % (TAB_SPACING);
280 break;
281 default:
282 (PCB).column++;
283 }
284 (PCB).read_flag = 1;
285 }
286
287
288 static void ag_prot(void) {
289 int ag_k;
290 ag_k = 128 - ++(PCB).btsx;
291 if (ag_k <= (PCB).ssx) {
292 (PCB).exit_flag = AG_STACK_ERROR_CODE;
293 PARSER_STACK_OVERFLOW;
294 return;
295 }
296 (PCB).bts[(PCB).btsx] = (PCB).sn;
297 (PCB).bts[ag_k] = (PCB).ssx;
298 (PCB).vs[ag_k] = (PCB).vs[(PCB).ssx];
299 (PCB).ss[ag_k] = (PCB).ss[(PCB).ssx];
300 (PCB).cs[ag_k] = (PCB).cs[(PCB).ssx];
301 }
302
303 static void ag_undo(void) {
304 if ((PCB).drt == -1) return;
305 while ((PCB).btsx) {
306 int ag_k = 128 - (PCB).btsx;
307 (PCB).sn = (PCB).bts[(PCB).btsx--];
308 (PCB).ssx = (PCB).bts[ag_k];
309 (PCB).vs[(PCB).ssx] = (PCB).vs[ag_k];
310 (PCB).ss[(PCB).ssx] = (PCB).ss[ag_k];
311 (PCB).cs[(PCB).ssx] = (PCB).cs[ag_k];
312 }
313 (PCB).token_number = (sort_token_type) (PCB).drt;
314 (PCB).ssx = (PCB).dssx;
315 (PCB).sn = (PCB).dsn;
316 (PCB).drt = -1;
317 }
318
319
320 static const unsigned char ag_tstt[] = {
321 16,10,5,3,0,4,8,12,13,14,15,
322 3,0,
323 10,5,0,1,2,6,7,9,
324 16,10,5,3,0,4,8,12,
325 16,0,
326 5,3,0,4,8,
327 5,0,1,2,6,7,
328 5,0,
329 16,10,5,3,0,4,8,
330 10,0,9,
331 5,0,1,2,6,
332 5,3,0,2,6,
333 11,3,0,4,8,
334 11,0,
335
336 };
337
338
339 static unsigned const char ag_astt[71] = {
340 8,8,8,1,7,1,1,1,0,1,1,9,5,1,1,7,1,2,1,1,1,5,8,8,1,7,1,1,3,3,7,8,1,7,1,1,1,
341 7,1,2,1,2,9,5,5,5,5,1,7,1,2,1,4,2,1,7,1,2,1,1,9,5,2,1,8,1,7,1,1,2,7
342 };
343
344
345 static const unsigned char ag_pstt[] = {
346 4,2,2,1,0,1,2,3,0,3,4,
347 3,9,
348 5,7,2,8,1,7,9,6,
349 18,2,2,1,3,1,2,16,
350 19,4,
351 10,1,5,1,10,
352 7,6,8,1,7,14,
353 6,7,
354 8,8,8,11,8,11,10,
355 5,12,13,
356 7,10,12,1,7,
357 7,3,9,4,7,
358 13,11,12,11,13,
359 11,13,
360
361 };
362
363
364 static const unsigned char ag_sbt[] = {
365 0, 11, 13, 21, 29, 31, 36, 42, 44, 51, 54, 59, 64, 69,
366 71
367 };
368
369
370 static const unsigned char ag_sbe[] = {
371 4, 12, 15, 25, 30, 33, 37, 43, 48, 52, 55, 61, 66, 70, 71
372 };
373
374
375 static const unsigned char ag_fl[] = {
376 1,1,1,2,3,1,2,1,0,1,2,5,2,3,3,1,2,0,1,2
377 };
378
379 static const unsigned char ag_ptt[] = {
380 0, 1, 4, 4, 1, 6, 6, 2, 8, 8, 7, 9, 12, 12, 12, 14, 14, 15,
381 15, 13
382 };
383
384
385 static void ag_ra(void)
386 {
387 switch(ag_rpx[(PCB).ag_ap]) {
388 case 1: V(0,(unsigned char * *)) = ag_rp_1(); break;
389 case 2: V(0,(String *)) = ag_rp_3(V(0,(unsigned char * *))); break;
390 case 3: V(0,(String *)) = ag_rp_4(V(2,(unsigned char * *))); break;
391 case 4: ag_rp_5(V(1,(String *))); break;
392 case 5: ag_rp_6(V(2,(String *))); break;
393 case 6: ag_rp_7(V(1,(String *))); break;
394 }
395 }
396
397 #define TOKEN_NAMES sort_token_names
398 const char *const sort_token_names[17] = {
399 "grammar",
400 "words",
401 "word",
402 "blank",
403 "",
404 "char",
405 "",
406 "text",
407 "",
408 "address",
409 "'<'",
410 "'>'",
411 "line",
412 "grammar",
413 "",
414 "",
415 "eof",
416
417 };
418
419 #ifndef MISSING_FORMAT
420 #define MISSING_FORMAT "Missing %s"
421 #endif
422 #ifndef UNEXPECTED_FORMAT
423 #define UNEXPECTED_FORMAT "Unexpected %s"
424 #endif
425 #ifndef UNNAMED_TOKEN
426 #define UNNAMED_TOKEN "input"
427 #endif
428
429
430 static void ag_diagnose(void) {
431 int ag_snd = (PCB).sn;
432 int ag_k = ag_sbt[ag_snd];
433
434 if (*TOKEN_NAMES[ag_tstt[ag_k]] && ag_astt[ag_k + 1] == ag_action_8) {
435 sprintf((PCB).ag_msg, MISSING_FORMAT, TOKEN_NAMES[ag_tstt[ag_k]]);
436 }
437 else if (ag_astt[ag_sbe[(PCB).sn]] == ag_action_8
438 && (ag_k = (int) ag_sbe[(PCB).sn] + 1) == (int) ag_sbt[(PCB).sn+1] - 1
439 && *TOKEN_NAMES[ag_tstt[ag_k]]) {
440 sprintf((PCB).ag_msg, MISSING_FORMAT, TOKEN_NAMES[ag_tstt[ag_k]]);
441 }
442 else if ((PCB).token_number && *TOKEN_NAMES[(PCB).token_number]) {
443 sprintf((PCB).ag_msg, UNEXPECTED_FORMAT, TOKEN_NAMES[(PCB).token_number]);
444 }
445 else if (isprint(((PCB).input_code)) && ((PCB).input_code) != '\\') {
446 char buf[20];
447 sprintf(buf, "\'%c\'", (char) ((PCB).input_code));
448 sprintf((PCB).ag_msg, UNEXPECTED_FORMAT, buf);
449 }
450 else sprintf((PCB).ag_msg, UNEXPECTED_FORMAT, UNNAMED_TOKEN);
451 (PCB).error_message = (PCB).ag_msg;
452
453
454 }
455 static int ag_action_1_r_proc(void);
456 static int ag_action_2_r_proc(void);
457 static int ag_action_3_r_proc(void);
458 static int ag_action_4_r_proc(void);
459 static int ag_action_1_s_proc(void);
460 static int ag_action_3_s_proc(void);
461 static int ag_action_1_proc(void);
462 static int ag_action_2_proc(void);
463 static int ag_action_3_proc(void);
464 static int ag_action_4_proc(void);
465 static int ag_action_5_proc(void);
466 static int ag_action_6_proc(void);
467 static int ag_action_7_proc(void);
468 static int ag_action_8_proc(void);
469 static int ag_action_9_proc(void);
470 static int ag_action_10_proc(void);
471 static int ag_action_11_proc(void);
472 static int ag_action_8_proc(void);
473
474
475 static int (*const ag_r_procs_scan[])(void) = {
476 ag_action_1_r_proc,
477 ag_action_2_r_proc,
478 ag_action_3_r_proc,
479 ag_action_4_r_proc
480 };
481
482 static int (*const ag_s_procs_scan[])(void) = {
483 ag_action_1_s_proc,
484 ag_action_2_r_proc,
485 ag_action_3_s_proc,
486 ag_action_4_r_proc
487 };
488
489 static int (*const ag_gt_procs_scan[])(void) = {
490 ag_action_1_proc,
491 ag_action_2_proc,
492 ag_action_3_proc,
493 ag_action_4_proc,
494 ag_action_5_proc,
495 ag_action_6_proc,
496 ag_action_7_proc,
497 ag_action_8_proc,
498 ag_action_9_proc,
499 ag_action_10_proc,
500 ag_action_11_proc,
501 ag_action_8_proc
502 };
503
504
505 static int ag_action_10_proc(void) {
506 int ag_t = (PCB).token_number;
507 (PCB).btsx = 0, (PCB).drt = -1;
508 do {
509 ag_track();
510 if ((PCB).read_flag) {
511 (PCB).read_flag = 0;
512 GET_INPUT;
513 };
514 (PCB).token_number = (sort_token_type) AG_TCV((PCB).input_code);
515 } while ((PCB).token_number == (sort_token_type) ag_t);
516 return 1;
517 }
518
519 static int ag_action_11_proc(void) {
520 int ag_t = (PCB).token_number;
521
522 (PCB).btsx = 0, (PCB).drt = -1;
523 do {
524 (*(int *) &(PCB).vs[(PCB).ssx]) = (PCB).input_code;
525 (PCB).ssx--;
526 ag_track();
527 ag_ra();
528 if ((PCB).exit_flag != AG_RUNNING_CODE) return 0;
529 (PCB).ssx++;
530 if ((PCB).read_flag) {
531 (PCB).read_flag = 0;
532 GET_INPUT;
533 };
534 (PCB).token_number = (sort_token_type) AG_TCV((PCB).input_code);
535 }
536 while ((PCB).token_number == (sort_token_type) ag_t);
537 return 1;
538 }
539
540 static int ag_action_3_r_proc(void) {
541 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
542 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
543 (PCB).btsx = 0, (PCB).drt = -1;
544 (PCB).reduction_token = (sort_token_type) ag_ptt[(PCB).ag_ap];
545 ag_ra();
546 return (PCB).exit_flag == AG_RUNNING_CODE;
547 }
548
549 static int ag_action_3_s_proc(void) {
550 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
551 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
552 (PCB).btsx = 0, (PCB).drt = -1;
553 (PCB).reduction_token = (sort_token_type) ag_ptt[(PCB).ag_ap];
554 ag_ra();
555 return (PCB).exit_flag == AG_RUNNING_CODE;
556 }
557
558 static int ag_action_4_r_proc(void) {
559 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
560 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
561 (PCB).reduction_token = (sort_token_type) ag_ptt[(PCB).ag_ap];
562 return 1;
563 }
564
565 static int ag_action_2_proc(void) {
566 (PCB).btsx = 0, (PCB).drt = -1;
567 if ((PCB).ssx >= 128) {
568 (PCB).exit_flag = AG_STACK_ERROR_CODE;
569 PARSER_STACK_OVERFLOW;
570 }
571 (*(int *) &(PCB).vs[(PCB).ssx]) = (PCB).input_code;
572 GET_CONTEXT;
573 (PCB).ss[(PCB).ssx] = (PCB).sn;
574 (PCB).ssx++;
575 (PCB).sn = (PCB).ag_ap;
576 ag_track();
577 return 0;
578 }
579
580 static int ag_action_9_proc(void) {
581 if ((PCB).drt == -1) {
582 (PCB).drt=(PCB).token_number;
583 (PCB).dssx=(PCB).ssx;
584 (PCB).dsn=(PCB).sn;
585 }
586 ag_prot();
587 (PCB).vs[(PCB).ssx] = ag_null_value;
588 GET_CONTEXT;
589 (PCB).ss[(PCB).ssx] = (PCB).sn;
590 (PCB).ssx++;
591 (PCB).sn = (PCB).ag_ap;
592 return (PCB).exit_flag == AG_RUNNING_CODE;
593 }
594
595 static int ag_action_2_r_proc(void) {
596 (PCB).ssx++;
597 (PCB).sn = (PCB).ag_ap;
598 return 0;
599 }
600
601 static int ag_action_7_proc(void) {
602 --(PCB).ssx;
603 (PCB).exit_flag = AG_SUCCESS_CODE;
604 return 0;
605 }
606
607 static int ag_action_1_proc(void) {
608 ag_track();
609 (PCB).exit_flag = AG_SUCCESS_CODE;
610 return 0;
611 }
612
613 static int ag_action_1_r_proc(void) {
614 (PCB).exit_flag = AG_SUCCESS_CODE;
615 return 0;
616 }
617
618 static int ag_action_1_s_proc(void) {
619 (PCB).exit_flag = AG_SUCCESS_CODE;
620 return 0;
621 }
622
623 static int ag_action_4_proc(void) {
624 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
625 (PCB).reduction_token = (sort_token_type) ag_ptt[(PCB).ag_ap];
626 (PCB).btsx = 0, (PCB).drt = -1;
627 (*(int *) &(PCB).vs[(PCB).ssx]) = (PCB).input_code;
628 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
629 else GET_CONTEXT;
630 (PCB).ss[(PCB).ssx] = (PCB).sn;
631 ag_track();
632 while ((PCB).exit_flag == AG_RUNNING_CODE) {
633 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1;
634 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1;
635 do {
636 unsigned ag_tx = (ag_t1 + ag_t2)/2;
637 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1;
638 else ag_t2 = ag_tx;
639 } while (ag_t1 < ag_t2);
640 (PCB).ag_ap = ag_pstt[ag_t1];
641 if ((ag_s_procs_scan[ag_astt[ag_t1]])() == 0) break;
642 }
643 return 0;
644 }
645
646 static int ag_action_3_proc(void) {
647 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
648 (PCB).btsx = 0, (PCB).drt = -1;
649 (*(int *) &(PCB).vs[(PCB).ssx]) = (PCB).input_code;
650 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
651 else GET_CONTEXT;
652 (PCB).ss[(PCB).ssx] = (PCB).sn;
653 ag_track();
654 (PCB).reduction_token = (sort_token_type) ag_ptt[(PCB).ag_ap];
655 ag_ra();
656 while ((PCB).exit_flag == AG_RUNNING_CODE) {
657 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1;
658 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1;
659 do {
660 unsigned ag_tx = (ag_t1 + ag_t2)/2;
661 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1;
662 else ag_t2 = ag_tx;
663 } while (ag_t1 < ag_t2);
664 (PCB).ag_ap = ag_pstt[ag_t1];
665 if ((ag_s_procs_scan[ag_astt[ag_t1]])() == 0) break;
666 }
667 return 0;
668 }
669
670 static int ag_action_8_proc(void) {
671 ag_undo();
672 (PCB).exit_flag = AG_SYNTAX_ERROR_CODE;
673 ag_diagnose();
674 SYNTAX_ERROR;
675 ag_track();
676 return (PCB).exit_flag == AG_RUNNING_CODE;
677 }
678
679 static int ag_action_5_proc(void) {
680 int ag_sd = ag_fl[(PCB).ag_ap];
681 (PCB).btsx = 0, (PCB).drt = -1;
682 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
683 else {
684 GET_CONTEXT;
685 (PCB).ss[(PCB).ssx] = (PCB).sn;
686 }
687 (PCB).reduction_token = (sort_token_type) ag_ptt[(PCB).ag_ap];
688 ag_ra();
689 while ((PCB).exit_flag == AG_RUNNING_CODE) {
690 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1;
691 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1;
692 do {
693 unsigned ag_tx = (ag_t1 + ag_t2)/2;
694 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1;
695 else ag_t2 = ag_tx;
696 } while (ag_t1 < ag_t2);
697 (PCB).ag_ap = ag_pstt[ag_t1];
698 if ((ag_r_procs_scan[ag_astt[ag_t1]])() == 0) break;
699 }
700 return (PCB).exit_flag == AG_RUNNING_CODE;
701 }
702
703 static int ag_action_6_proc(void) {
704 int ag_sd = ag_fl[(PCB).ag_ap];
705 (PCB).reduction_token = (sort_token_type) ag_ptt[(PCB).ag_ap];
706 if ((PCB).drt == -1) {
707 (PCB).drt=(PCB).token_number;
708 (PCB).dssx=(PCB).ssx;
709 (PCB).dsn=(PCB).sn;
710 }
711 if (ag_sd) {
712 (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
713 }
714 else {
715 ag_prot();
716 (PCB).vs[(PCB).ssx] = ag_null_value;
717 GET_CONTEXT;
718 (PCB).ss[(PCB).ssx] = (PCB).sn;
719 }
720 while ((PCB).exit_flag == AG_RUNNING_CODE) {
721 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1;
722 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1;
723 do {
724 unsigned ag_tx = (ag_t1 + ag_t2)/2;
725 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1;
726 else ag_t2 = ag_tx;
727 } while (ag_t1 < ag_t2);
728 (PCB).ag_ap = ag_pstt[ag_t1];
729 if ((ag_r_procs_scan[ag_astt[ag_t1]])() == 0) break;
730 }
731 return (PCB).exit_flag == AG_RUNNING_CODE;
732 }
733
734
735 void init_sort(void) {
736 (PCB).read_flag = 1;
737 (PCB).ss[0] = (PCB).sn = (PCB).ssx = 0;
738 (PCB).exit_flag = AG_RUNNING_CODE;
739 (PCB).line = FIRST_LINE;
740 (PCB).column = FIRST_COLUMN;
741 (PCB).btsx = 0, (PCB).drt = -1;
742 }
743
744 void sort(void) {
745 init_sort();
746 (PCB).exit_flag = AG_RUNNING_CODE;
747 while ((PCB).exit_flag == AG_RUNNING_CODE) {
748 unsigned ag_t1 = ag_sbt[(PCB).sn];
749 if (ag_tstt[ag_t1]) {
750 unsigned ag_t2 = ag_sbe[(PCB).sn] - 1;
751 if ((PCB).read_flag) {
752 (PCB).read_flag = 0;
753 GET_INPUT;
754 };
755 (PCB).token_number = (sort_token_type) AG_TCV((PCB).input_code);
756 do {
757 unsigned ag_tx = (ag_t1 + ag_t2)/2;
758 if (ag_tstt[ag_tx] > (unsigned char)(PCB).token_number)
759 ag_t1 = ag_tx + 1;
760 else ag_t2 = ag_tx;
761 } while (ag_t1 < ag_t2);
762 if (ag_tstt[ag_t1] != (unsigned char)(PCB).token_number)
763 ag_t1 = ag_sbe[(PCB).sn];
764 }
765 (PCB).ag_ap = ag_pstt[ag_t1];
766 (ag_gt_procs_scan[ag_astt[ag_t1]])();
767 }
768 }
769
770