Mercurial > ~dholland > hg > ag > index.cgi
comparison tests/agcl/examples/good/ex.cpp @ 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 /* | |
2 * AnaGram, a System for Syntax Directed Programming | |
3 * C Macro preprocessor | |
4 * Constant Expression Evaluator Module | |
5 * | |
6 * Copyright 1993-2000 Parsifal Software. All Rights Reserved. | |
7 * | |
8 * This software is provided 'as-is', without any express or implied | |
9 * warranty. In no event will the authors be held liable for any damages | |
10 * arising from the use of this software. | |
11 * | |
12 * Permission is granted to anyone to use this software for any purpose, | |
13 * including commercial applications, and to alter it and redistribute it | |
14 * freely, subject to the following restrictions: | |
15 * | |
16 * 1. The origin of this software must not be misrepresented; you must not | |
17 * claim that you wrote the original software. If you use this software | |
18 * in a product, an acknowledgment in the product documentation would be | |
19 * appreciated but is not required. | |
20 * 2. Altered source versions must be plainly marked as such, and must not be | |
21 * misrepresented as being the original software. | |
22 * 3. This notice may not be removed or altered from any source distribution. | |
23 */ | |
24 | |
25 #include "mpp.h" | |
26 | |
27 /* | |
28 * AnaGram, A System for Syntax Directed Programming | |
29 * File generated by: ... | |
30 * | |
31 * AnaGram Parsing Engine | |
32 * Copyright 1993-2002 Parsifal Software. All Rights Reserved. | |
33 * | |
34 * This software is provided 'as-is', without any express or implied | |
35 * warranty. In no event will the authors be held liable for any damages | |
36 * arising from the use of this software. | |
37 * | |
38 * Permission is granted to anyone to use this software for any purpose, | |
39 * including commercial applications, and to alter it and redistribute it | |
40 * freely, subject to the following restrictions: | |
41 * | |
42 * 1. The origin of this software must not be misrepresented; you must not | |
43 * claim that you wrote the original software. If you use this software | |
44 * in a product, an acknowledgment in the product documentation would be | |
45 * appreciated but is not required. | |
46 * 2. Altered source versions must be plainly marked as such, and must not be | |
47 * misrepresented as being the original software. | |
48 * 3. This notice may not be removed or altered from any source distribution. | |
49 */ | |
50 | |
51 #ifndef EX_H | |
52 #include "ex.h" | |
53 #endif | |
54 | |
55 #ifndef EX_H | |
56 #error Mismatched header file | |
57 #endif | |
58 | |
59 #include <stdio.h> | |
60 | |
61 #define RULE_CONTEXT (&((PCB).cs[(PCB).ssx])) | |
62 #define ERROR_CONTEXT ((PCB).cs[(PCB).error_frame_ssx]) | |
63 #define CONTEXT ((PCB).cs[(PCB).ssx]) | |
64 | |
65 | |
66 | |
67 long ex_value(void); | |
68 | |
69 #define INPUT_VALUE(type) *(type *) &(PCB).input_value | |
70 | |
71 #line - "ex.syn" | |
72 // Embedded C | |
73 #include <stdio.h> | |
74 | |
75 | |
76 // Macro Definitions | |
77 | |
78 #define PCB (*ex_pcb) | |
79 #define SYNTAX_ERROR syntax_error("Syntax error in constant expression"); | |
80 | |
81 | |
82 // Static data | |
83 | |
84 static ex_pcb_type *ex_pcb; // Pointer to active pcb | |
85 | |
86 | |
87 /* | |
88 | |
89 decode() is a simple function to recover the numeric value of a | |
90 token. Its task is somewhat simplified by the fact that the only | |
91 values allowed in this example are longs. | |
92 | |
93 */ | |
94 | |
95 static long decode(const char *fmt, unsigned ndx) { | |
96 long v; | |
97 sscanf(td[ndx], fmt, &v); | |
98 return v; | |
99 } | |
100 | |
101 | |
102 // Members of expression_evaluator Class | |
103 | |
104 | |
105 // Constructor | |
106 | |
107 /* | |
108 Each expression_evaluator has its own parser control block. | |
109 Therefore, before calling ex() or its initializer init_ex() it is | |
110 necessary to make sure the pcb pointer for ex() is correctly set. | |
111 */ | |
112 | |
113 expression_evaluator::expression_evaluator() { | |
114 ex_pcb = &pcb; // set up pointer to pcb | |
115 init_ex(); // init parser | |
116 } | |
117 | |
118 | |
119 // Reset expression_evaluator | |
120 | |
121 token_sink& reset(expression_evaluator &x) { | |
122 ex_pcb = &x.pcb; // set up pointer to pcb | |
123 init_ex(); // init parser | |
124 return x; | |
125 } | |
126 | |
127 | |
128 // Xmit Token to expression_evaluator | |
129 | |
130 /* | |
131 There could be space tokens in the input to the expression | |
132 evaluator, but they are never significant so it is acceptable to | |
133 filter them out on input. | |
134 | |
135 If the expression_evaluator has encountered an error, there is no | |
136 point in giving it any further input. | |
137 | |
138 Otherwise, the input_code and input_value fields of the pcb are set | |
139 up, the pcb pointer for ex is set, and ex() is called to deal with | |
140 the token. | |
141 | |
142 When looping, it is only necessary to set up ex_pcb at the | |
143 beginning of the loop. | |
144 */ | |
145 | |
146 token_sink& expression_evaluator::operator << (token t) { | |
147 if (t.id == SPACE || pcb.exit_flag != AG_RUNNING_CODE) return *this; | |
148 pcb.input_code = t.id; | |
149 pcb.input_value = t; | |
150 ex_pcb = &pcb; | |
151 ex(); | |
152 return *this; | |
153 } | |
154 | |
155 token_sink &expression_evaluator::operator << (token *tp) { | |
156 ex_pcb = &pcb; | |
157 while (pcb.exit_flag == AG_RUNNING_CODE) { | |
158 if (pcb.input_code == SPACE) continue; | |
159 pcb.input_code = tp->id; | |
160 pcb.input_value = *tp++; | |
161 ex(); | |
162 } | |
163 return *this; | |
164 } | |
165 | |
166 | |
167 // Return Value of expression_evaluator | |
168 | |
169 /* | |
170 The (long) cast operator is overloaded to provide a mechanism to | |
171 retrieve the value of the expression evaluated by the | |
172 expression_evaluator. | |
173 | |
174 If there was a syntax error, the return value is zero. | |
175 */ | |
176 | |
177 expression_evaluator::operator long() { | |
178 ex_pcb = &pcb; | |
179 if (pcb.exit_flag == AG_SUCCESS_CODE) return ex_value(); | |
180 else return 0; | |
181 } | |
182 | |
183 /* | |
184 // Check expression_evaluator for error | |
185 | |
186 int error(expression_evaluator &x) { | |
187 return x.pcb.exit_flag != AG_SUCCESS_CODE; | |
188 } | |
189 */ | |
190 #line - "ex.cpp" | |
191 | |
192 #ifndef CONVERT_CASE | |
193 #define CONVERT_CASE(c) (c) | |
194 #endif | |
195 #ifndef TAB_SPACING | |
196 #define TAB_SPACING 8 | |
197 #endif | |
198 long ex_value(void) { | |
199 long returnValue; | |
200 returnValue = (*(long *) &(PCB).vs[(PCB).ssx]); | |
201 return returnValue; | |
202 } | |
203 | |
204 static long ag_rp_1(long c, long x, long y) { | |
205 #line - "ex.syn" | |
206 return c != 0 ? x : y; | |
207 #line - "ex.cpp" | |
208 } | |
209 | |
210 static long ag_rp_2(long x, long y) { | |
211 #line - "ex.syn" | |
212 return x != 0 || y!=0; | |
213 #line - "ex.cpp" | |
214 } | |
215 | |
216 static long ag_rp_3(long x, long y) { | |
217 #line - "ex.syn" | |
218 return x != 0 && y !=0; | |
219 #line - "ex.cpp" | |
220 } | |
221 | |
222 static long ag_rp_4(long x, long y) { | |
223 #line - "ex.syn" | |
224 return x | y; | |
225 #line - "ex.cpp" | |
226 } | |
227 | |
228 static long ag_rp_5(long x, long y) { | |
229 #line - "ex.syn" | |
230 return x ^ y; | |
231 #line - "ex.cpp" | |
232 } | |
233 | |
234 static long ag_rp_6(long x, long y) { | |
235 #line - "ex.syn" | |
236 return x & y; | |
237 #line - "ex.cpp" | |
238 } | |
239 | |
240 static long ag_rp_7(long x, long y) { | |
241 #line - "ex.syn" | |
242 return x == y; | |
243 #line - "ex.cpp" | |
244 } | |
245 | |
246 static long ag_rp_8(long x, long y) { | |
247 #line - "ex.syn" | |
248 return x != y; | |
249 #line - "ex.cpp" | |
250 } | |
251 | |
252 static long ag_rp_9(long x, long y) { | |
253 #line - "ex.syn" | |
254 return x < y; | |
255 #line - "ex.cpp" | |
256 } | |
257 | |
258 static long ag_rp_10(long x, long y) { | |
259 #line - "ex.syn" | |
260 return x > y; | |
261 #line - "ex.cpp" | |
262 } | |
263 | |
264 static long ag_rp_11(long x, long y) { | |
265 #line - "ex.syn" | |
266 return x <= y; | |
267 #line - "ex.cpp" | |
268 } | |
269 | |
270 static long ag_rp_12(long x, long y) { | |
271 #line - "ex.syn" | |
272 return x >= y; | |
273 #line - "ex.cpp" | |
274 } | |
275 | |
276 static long ag_rp_13(long x, long y) { | |
277 #line - "ex.syn" | |
278 return x << y; | |
279 #line - "ex.cpp" | |
280 } | |
281 | |
282 static long ag_rp_14(long x, long y) { | |
283 #line - "ex.syn" | |
284 return x >> y; | |
285 #line - "ex.cpp" | |
286 } | |
287 | |
288 static long ag_rp_15(long x, long y) { | |
289 #line - "ex.syn" | |
290 return x + y; | |
291 #line - "ex.cpp" | |
292 } | |
293 | |
294 static long ag_rp_16(long x, long y) { | |
295 #line - "ex.syn" | |
296 return x - y; | |
297 #line - "ex.cpp" | |
298 } | |
299 | |
300 static long ag_rp_17(long x, long y) { | |
301 #line - "ex.syn" | |
302 return x * y; | |
303 #line - "ex.cpp" | |
304 } | |
305 | |
306 static long ag_rp_18(long x, long y) { | |
307 #line - "ex.syn" | |
308 return x / y; | |
309 #line - "ex.cpp" | |
310 } | |
311 | |
312 static long ag_rp_19(long x, long y) { | |
313 #line - "ex.syn" | |
314 return x % y; | |
315 #line - "ex.cpp" | |
316 } | |
317 | |
318 static long ag_rp_20(long x) { | |
319 #line - "ex.syn" | |
320 if (x != 0) return x; | |
321 syntax_error("Divide by Zero"); | |
322 PCB.exit_flag = 5; | |
323 return x; | |
324 | |
325 #line - "ex.cpp" | |
326 } | |
327 | |
328 static long ag_rp_21(long x) { | |
329 #line - "ex.syn" | |
330 return x; | |
331 #line - "ex.cpp" | |
332 } | |
333 | |
334 static long ag_rp_22(long x) { | |
335 #line - "ex.syn" | |
336 return -x; | |
337 #line - "ex.cpp" | |
338 } | |
339 | |
340 static long ag_rp_23(long x) { | |
341 #line - "ex.syn" | |
342 return ~x; | |
343 #line - "ex.cpp" | |
344 } | |
345 | |
346 static long ag_rp_24(long x) { | |
347 #line - "ex.syn" | |
348 return !x; | |
349 #line - "ex.cpp" | |
350 } | |
351 | |
352 static long ag_rp_25(token x) { | |
353 #line - "ex.syn" | |
354 return decode("%lx",x.handle); | |
355 #line - "ex.cpp" | |
356 } | |
357 | |
358 static long ag_rp_26(token x) { | |
359 #line - "ex.syn" | |
360 return decode("%lo",x.handle); | |
361 #line - "ex.cpp" | |
362 } | |
363 | |
364 static long ag_rp_27(token x) { | |
365 #line - "ex.syn" | |
366 return decode("%ld",x.handle); | |
367 #line - "ex.cpp" | |
368 } | |
369 | |
370 static long ag_rp_28(token x) { | |
371 #line - "ex.syn" | |
372 return decode("'%lc'",x.handle); | |
373 #line - "ex.cpp" | |
374 } | |
375 | |
376 static long ag_rp_29(void) { | |
377 #line - "ex.syn" | |
378 return 0; | |
379 #line - "ex.cpp" | |
380 } | |
381 | |
382 static long ag_rp_30(long x) { | |
383 #line - "ex.syn" | |
384 return x; | |
385 #line - "ex.cpp" | |
386 } | |
387 | |
388 | |
389 #ifndef AG_TRACE_FILE_NAME | |
390 #define AG_TRACE_FILE_NAME "ex.etr" | |
391 #endif | |
392 | |
393 static void ag_trace_error(void) { | |
394 FILE *ag_file = fopen(AG_TRACE_FILE_NAME, "w"); | |
395 int i; | |
396 if (ag_file == NULL) return; | |
397 fprintf(ag_file, "%d\n", (PCB).ssx); | |
398 for (i = 0; i < (PCB).ssx; i++) fprintf(ag_file, "%d\n", (PCB).ss[i]); | |
399 fprintf(ag_file, "%d\n", (PCB).sn); | |
400 fprintf(ag_file, "%d\n", (PCB).token_number); | |
401 fclose(ag_file); | |
402 } | |
403 | |
404 | |
405 #define READ_COUNTS | |
406 #define WRITE_COUNTS | |
407 #undef V | |
408 #define V(i,t) (*t (&(PCB).vs[(PCB).ssx + i])) | |
409 #undef VS | |
410 #define VS(i) (PCB).vs[(PCB).ssx + i] | |
411 | |
412 #ifndef GET_CONTEXT | |
413 #define GET_CONTEXT CONTEXT = (PCB).input_context | |
414 #endif | |
415 | |
416 typedef enum { | |
417 ag_action_1, | |
418 ag_action_2, | |
419 ag_action_3, | |
420 ag_action_4, | |
421 ag_action_5, | |
422 ag_action_6, | |
423 ag_action_7, | |
424 ag_action_8, | |
425 ag_action_9, | |
426 ag_action_10, | |
427 ag_action_11, | |
428 ag_action_12 | |
429 } ag_parser_action; | |
430 | |
431 | |
432 #ifndef NULL_VALUE_INITIALIZER | |
433 #define NULL_VALUE_INITIALIZER = { 0 } | |
434 #endif | |
435 | |
436 static ex_vs_type const ag_null_value NULL_VALUE_INITIALIZER; | |
437 | |
438 static const unsigned char ag_rpx[] = { | |
439 0, 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 8, | |
440 0, 9, 10, 11, 12, 0, 13, 14, 0, 15, 16, 0, 17, 18, 19, 20, 0, 21, | |
441 22, 23, 24, 25, 26, 27, 28, 29, 30 | |
442 }; | |
443 | |
444 #define AG_TCV(x) ag_tcv[(x)] | |
445 | |
446 static const unsigned char ag_tcv[] = { | |
447 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
448 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 39, 0, 0, | |
449 0, 36, 17, 0, 45, 46, 33, 30, 0, 31, 0, 34, 0, 0, 0, 0, 0, 0, | |
450 0, 0, 0, 0, 7, 0, 22, 0, 23, 6, 0, 11, 0, 0, 0, 0, 0, 0, | |
451 19, 0, 25, 0, 24, 27, 0, 0, 0, 0, 20, 0, 9, 0, 28, 0, 43, 0, | |
452 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
453 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, | |
454 38, 0, 0, 40, 41, 42, 0, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
455 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
456 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
457 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
458 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
459 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
460 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
461 0, 0, 0, 0 | |
462 }; | |
463 | |
464 #ifndef SYNTAX_ERROR | |
465 #define SYNTAX_ERROR fprintf(stderr,"%s\n", (PCB).error_message) | |
466 #endif | |
467 | |
468 #ifndef PARSER_STACK_OVERFLOW | |
469 #define PARSER_STACK_OVERFLOW {fprintf(stderr, \ | |
470 "\nParser stack overflow\n");} | |
471 #endif | |
472 | |
473 #ifndef REDUCTION_TOKEN_ERROR | |
474 #define REDUCTION_TOKEN_ERROR {fprintf(stderr, \ | |
475 "\nReduction token error\n");} | |
476 #endif | |
477 | |
478 | |
479 static void ag_prot(void) { | |
480 int ag_k; | |
481 ag_k = 128 - ++(PCB).btsx; | |
482 if (ag_k <= (PCB).ssx) { | |
483 ag_trace_error(); | |
484 (PCB).exit_flag = AG_STACK_ERROR_CODE; | |
485 PARSER_STACK_OVERFLOW; | |
486 return; | |
487 } | |
488 (PCB).bts[(PCB).btsx] = (PCB).sn; | |
489 (PCB).bts[ag_k] = (PCB).ssx; | |
490 (PCB).vs[ag_k] = (PCB).vs[(PCB).ssx]; | |
491 (PCB).ss[ag_k] = (PCB).ss[(PCB).ssx]; | |
492 } | |
493 | |
494 static void ag_undo(void) { | |
495 if ((PCB).drt == -1) return; | |
496 while ((PCB).btsx) { | |
497 int ag_k = 128 - (PCB).btsx; | |
498 (PCB).sn = (PCB).bts[(PCB).btsx--]; | |
499 (PCB).ssx = (PCB).bts[ag_k]; | |
500 (PCB).vs[(PCB).ssx] = (PCB).vs[ag_k]; | |
501 (PCB).ss[(PCB).ssx] = (PCB).ss[ag_k]; | |
502 } | |
503 (PCB).token_number = (ex_token_type) (PCB).drt; | |
504 (PCB).ssx = (PCB).dssx; | |
505 (PCB).sn = (PCB).dsn; | |
506 (PCB).drt = -1; | |
507 } | |
508 | |
509 | |
510 static const unsigned char ag_tstt[] = { | |
511 45,44,43,42,41,40,39,38,31,30,0,1,2,4,5,8,10,12,14,16,18,21,26,29,32,37, | |
512 45,44,43,42,41,40,39,38,31,30,0,4,5,8,10,12,14,16,18,21,26,29,32,37, | |
513 45,44,43,42,41,40,39,38,31,30,0,32,37, | |
514 45,44,43,42,41,40,39,38,31,30,0,32,37, | |
515 45,44,43,42,41,40,39,38,31,30,0,32,37, | |
516 45,44,43,42,41,40,39,38,31,30,0,32,37, | |
517 36,34,33,0, | |
518 31,30,0, | |
519 28,27,0, | |
520 25,24,23,22,0, | |
521 20,19,0, | |
522 17,0, | |
523 15,0, | |
524 13,0, | |
525 11,0, | |
526 9,6,0, | |
527 3,0, | |
528 46,0, | |
529 45,44,43,42,41,40,39,38,31,30,0,32,35,37, | |
530 45,44,43,42,41,40,39,38,31,30,0,32,35,37, | |
531 45,44,43,42,41,40,39,38,31,30,0,32,37, | |
532 45,44,43,42,41,40,39,38,31,30,0,29,32,37, | |
533 45,44,43,42,41,40,39,38,31,30,0,29,32,37, | |
534 45,44,43,42,41,40,39,38,31,30,0,26,29,32,37, | |
535 45,44,43,42,41,40,39,38,31,30,0,26,29,32,37, | |
536 45,44,43,42,41,40,39,38,31,30,0,21,26,29,32,37, | |
537 45,44,43,42,41,40,39,38,31,30,0,21,26,29,32,37, | |
538 45,44,43,42,41,40,39,38,31,30,0,21,26,29,32,37, | |
539 45,44,43,42,41,40,39,38,31,30,0,21,26,29,32,37, | |
540 45,44,43,42,41,40,39,38,31,30,0,18,21,26,29,32,37, | |
541 45,44,43,42,41,40,39,38,31,30,0,18,21,26,29,32,37, | |
542 45,44,43,42,41,40,39,38,31,30,0,16,18,21,26,29,32,37, | |
543 45,44,43,42,41,40,39,38,31,30,0,14,16,18,21,26,29,32,37, | |
544 45,44,43,42,41,40,39,38,31,30,0,12,14,16,18,21,26,29,32,37, | |
545 45,44,43,42,41,40,39,38,31,30,0,10,12,14,16,18,21,26,29,32,37, | |
546 45,44,43,42,41,40,39,38,31,30,0,8,10,12,14,16,18,21,26,29,32,37, | |
547 45,44,43,42,41,40,39,38,31,30,0,4,5,8,10,12,14,16,18,21,26,29,32,37, | |
548 36,34,33,0, | |
549 36,34,33,0, | |
550 31,30,0, | |
551 31,30,0, | |
552 28,27,0, | |
553 28,27,0, | |
554 28,27,0, | |
555 28,27,0, | |
556 25,24,23,22,0, | |
557 25,24,23,22,0, | |
558 20,19,0, | |
559 17,0, | |
560 15,0, | |
561 13,0, | |
562 11,0, | |
563 7,0, | |
564 45,44,43,42,41,40,39,38,31,30,0,4,5,8,10,12,14,16,18,21,26,29,32,37, | |
565 0 | |
566 }; | |
567 | |
568 | |
569 static unsigned const char ag_astt[530] = { | |
570 1,2,2,2,2,2,1,1,1,1,7,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,1,1,1,1,7, | |
571 1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,1,1,1,1,7,2,2,1,2,2,2,2,2,1,1,1,1,7, | |
572 2,2,1,2,2,2,2,2,1,1,1,1,7,2,2,1,2,2,2,2,2,1,1,1,1,7,2,2,1,1,1,5,1,1,5,1,1, | |
573 5,1,1,1,1,5,1,1,5,1,5,1,5,1,5,1,5,1,1,5,3,7,2,7,1,2,2,2,2,2,1,1,1,1,7,2,2, | |
574 2,1,2,2,2,2,2,1,1,1,1,7,2,2,2,1,2,2,2,2,2,1,1,1,1,7,2,2,1,2,2,2,2,2,1,1,1, | |
575 1,7,1,1,1,1,2,2,2,2,2,1,1,1,1,7,1,1,1,1,2,2,2,2,2,1,1,1,1,7,1,1,1,1,1,2,2, | |
576 2,2,2,1,1,1,1,7,1,1,1,1,1,2,2,2,2,2,1,1,1,1,7,1,1,1,1,1,1,2,2,2,2,2,1,1,1, | |
577 1,7,1,1,1,1,1,1,2,2,2,2,2,1,1,1,1,7,1,1,1,1,1,1,2,2,2,2,2,1,1,1,1,7,1,1,1, | |
578 1,1,1,2,2,2,2,2,1,1,1,1,7,1,1,1,1,1,1,1,2,2,2,2,2,1,1,1,1,7,1,1,1,1,1,1,1, | |
579 2,2,2,2,2,1,1,1,1,7,1,1,1,1,1,1,1,1,2,2,2,2,2,1,1,1,1,7,1,1,1,1,1,1,1,1,1, | |
580 2,2,2,2,2,1,1,1,1,7,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,1,1,1,1,7,1,1,1,1,1,1,1, | |
581 1,1,1,1,2,2,2,2,2,1,1,1,1,7,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,1,1,1,1,7,1, | |
582 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,1,1,1,4,1,1,4,1,1,4,1,1,4,1,1,4,1,1,4,1,1, | |
583 4,1,1,1,1,4,1,1,1,1,4,1,1,4,1,4,1,4,1,4,1,4,1,7,1,2,2,2,2,2,1,1,1,1,7,2,1, | |
584 1,1,1,1,1,1,1,1,1,1,1,11 | |
585 }; | |
586 | |
587 | |
588 static const unsigned char ag_pstt[] = { | |
589 1,43,42,41,40,39,2,3,4,5,0,0,16,16,15,14,13,12,11,10,9,8,7,6,6,6, | |
590 1,43,42,41,40,39,2,3,4,5,1,17,15,14,13,12,11,10,9,8,7,6,6,6, | |
591 1,43,42,41,40,39,2,3,4,5,2,38,38, | |
592 1,43,42,41,40,39,2,3,4,5,3,37,37, | |
593 1,43,42,41,40,39,2,3,4,5,4,36,36, | |
594 1,43,42,41,40,39,2,3,4,5,5,35,35, | |
595 18,19,20,26, | |
596 21,22,23, | |
597 23,24,18, | |
598 25,26,27,28,15, | |
599 29,30,13, | |
600 31,11, | |
601 32,9, | |
602 33,7, | |
603 34,5, | |
604 35,36,3, | |
605 1,16, | |
606 44,17, | |
607 1,43,42,41,40,39,2,3,4,5,18,33,32,33, | |
608 1,43,42,41,40,39,2,3,4,5,19,33,31,33, | |
609 1,43,42,41,40,39,2,3,4,5,20,30,30, | |
610 1,43,42,41,40,39,2,3,4,5,21,37,37,37, | |
611 1,43,42,41,40,39,2,3,4,5,22,38,38,38, | |
612 1,43,42,41,40,39,2,3,4,5,23,39,6,6,6, | |
613 1,43,42,41,40,39,2,3,4,5,24,40,6,6,6, | |
614 1,43,42,41,40,39,2,3,4,5,25,41,7,6,6,6, | |
615 1,43,42,41,40,39,2,3,4,5,26,42,7,6,6,6, | |
616 1,43,42,41,40,39,2,3,4,5,27,43,7,6,6,6, | |
617 1,43,42,41,40,39,2,3,4,5,28,44,7,6,6,6, | |
618 1,43,42,41,40,39,2,3,4,5,29,45,8,7,6,6,6, | |
619 1,43,42,41,40,39,2,3,4,5,30,46,8,7,6,6,6, | |
620 1,43,42,41,40,39,2,3,4,5,31,47,9,8,7,6,6,6, | |
621 1,43,42,41,40,39,2,3,4,5,32,48,10,9,8,7,6,6,6, | |
622 1,43,42,41,40,39,2,3,4,5,33,49,11,10,9,8,7,6,6,6, | |
623 1,43,42,41,40,39,2,3,4,5,34,50,12,11,10,9,8,7,6,6,6, | |
624 1,43,42,41,40,39,2,3,4,5,35,51,13,12,11,10,9,8,7,6,6,6, | |
625 1,43,42,41,40,39,2,3,4,5,36,52,15,14,13,12,11,10,9,8,7,6,6,6, | |
626 18,19,20,28, | |
627 18,19,20,27, | |
628 21,22,25, | |
629 21,22,24, | |
630 23,24,22, | |
631 23,24,21, | |
632 23,24,20, | |
633 23,24,19, | |
634 25,26,27,28,17, | |
635 25,26,27,28,16, | |
636 29,30,14, | |
637 31,12, | |
638 32,10, | |
639 33,8, | |
640 34,6, | |
641 53,52, | |
642 1,43,42,41,40,39,2,3,4,5,53,4,15,14,13,12,11,10,9,8,7,6,6,6, | |
643 0 | |
644 }; | |
645 | |
646 | |
647 static const unsigned short ag_sbt[] = { | |
648 0, 26, 50, 63, 76, 89, 102, 106, 109, 112, 117, 120, 122, 124, | |
649 126, 128, 131, 133, 135, 149, 163, 176, 190, 204, 219, 234, 250, 266, | |
650 282, 298, 315, 332, 350, 369, 389, 410, 432, 456, 460, 464, 467, 470, | |
651 473, 476, 479, 482, 487, 492, 495, 497, 499, 501, 503, 505, 529 | |
652 }; | |
653 | |
654 | |
655 static const unsigned short ag_sbe[] = { | |
656 10, 36, 60, 73, 86, 99, 105, 108, 111, 116, 119, 121, 123, 125, | |
657 127, 130, 132, 134, 145, 159, 173, 186, 200, 214, 229, 244, 260, 276, | |
658 292, 308, 325, 342, 360, 379, 399, 420, 442, 459, 463, 466, 469, 472, | |
659 475, 478, 481, 486, 491, 494, 496, 498, 500, 502, 504, 515, 529 | |
660 }; | |
661 | |
662 | |
663 static const unsigned char ag_fl[] = { | |
664 1,2,1,1,5,1,3,1,3,1,3,1,3,1,3,1,3,3,1,3,3,3,3,1,3,3,1,3,3,1,3,3,3,1,1, | |
665 2,2,2,2,1,1,1,1,1,3 | |
666 }; | |
667 | |
668 static const unsigned char ag_ptt[] = { | |
669 0, 1, 2, 4, 4, 5, 5, 8, 8, 10, 10, 12, 12, 14, 14, 16, 16, 16, | |
670 18, 18, 18, 18, 18, 21, 21, 21, 26, 26, 26, 29, 29, 29, 29, 35, 32, 32, | |
671 32, 32, 32, 37, 37, 37, 37, 37, 37 | |
672 }; | |
673 | |
674 | |
675 static void ag_ra(void) | |
676 { | |
677 switch(ag_rpx[(PCB).ag_ap]) { | |
678 case 1: V(0,(long *)) = ag_rp_1(V(0,(long *)), V(2,(long *)), V(4,(long *))); break; | |
679 case 2: V(0,(long *)) = ag_rp_2(V(0,(long *)), V(2,(long *))); break; | |
680 case 3: V(0,(long *)) = ag_rp_3(V(0,(long *)), V(2,(long *))); break; | |
681 case 4: V(0,(long *)) = ag_rp_4(V(0,(long *)), V(2,(long *))); break; | |
682 case 5: V(0,(long *)) = ag_rp_5(V(0,(long *)), V(2,(long *))); break; | |
683 case 6: V(0,(long *)) = ag_rp_6(V(0,(long *)), V(2,(long *))); break; | |
684 case 7: V(0,(long *)) = ag_rp_7(V(0,(long *)), V(2,(long *))); break; | |
685 case 8: V(0,(long *)) = ag_rp_8(V(0,(long *)), V(2,(long *))); break; | |
686 case 9: V(0,(long *)) = ag_rp_9(V(0,(long *)), V(2,(long *))); break; | |
687 case 10: V(0,(long *)) = ag_rp_10(V(0,(long *)), V(2,(long *))); break; | |
688 case 11: V(0,(long *)) = ag_rp_11(V(0,(long *)), V(2,(long *))); break; | |
689 case 12: V(0,(long *)) = ag_rp_12(V(0,(long *)), V(2,(long *))); break; | |
690 case 13: V(0,(long *)) = ag_rp_13(V(0,(long *)), V(2,(long *))); break; | |
691 case 14: V(0,(long *)) = ag_rp_14(V(0,(long *)), V(2,(long *))); break; | |
692 case 15: V(0,(long *)) = ag_rp_15(V(0,(long *)), V(2,(long *))); break; | |
693 case 16: V(0,(long *)) = ag_rp_16(V(0,(long *)), V(2,(long *))); break; | |
694 case 17: V(0,(long *)) = ag_rp_17(V(0,(long *)), V(2,(long *))); break; | |
695 case 18: V(0,(long *)) = ag_rp_18(V(0,(long *)), V(2,(long *))); break; | |
696 case 19: V(0,(long *)) = ag_rp_19(V(0,(long *)), V(2,(long *))); break; | |
697 case 20: V(0,(long *)) = ag_rp_20(V(0,(long *))); break; | |
698 case 21: V(0,(long *)) = ag_rp_21(V(1,(long *))); break; | |
699 case 22: V(0,(long *)) = ag_rp_22(V(1,(long *))); break; | |
700 case 23: V(0,(long *)) = ag_rp_23(V(1,(long *))); break; | |
701 case 24: V(0,(long *)) = ag_rp_24(V(1,(long *))); break; | |
702 case 25: V(0,(long *)) = ag_rp_25(V(0,(token *))); break; | |
703 case 26: V(0,(long *)) = ag_rp_26(V(0,(token *))); break; | |
704 case 27: V(0,(long *)) = ag_rp_27(V(0,(token *))); break; | |
705 case 28: V(0,(long *)) = ag_rp_28(V(0,(token *))); break; | |
706 case 29: V(0,(long *)) = ag_rp_29(); break; | |
707 case 30: V(0,(long *)) = ag_rp_30(V(1,(long *))); break; | |
708 } | |
709 } | |
710 | |
711 static int ag_action_1_r_proc(void); | |
712 static int ag_action_2_r_proc(void); | |
713 static int ag_action_3_r_proc(void); | |
714 static int ag_action_4_r_proc(void); | |
715 static int ag_action_1_s_proc(void); | |
716 static int ag_action_3_s_proc(void); | |
717 static int ag_action_1_proc(void); | |
718 static int ag_action_2_proc(void); | |
719 static int ag_action_3_proc(void); | |
720 static int ag_action_4_proc(void); | |
721 static int ag_action_5_proc(void); | |
722 static int ag_action_6_proc(void); | |
723 static int ag_action_7_proc(void); | |
724 static int ag_action_8_proc(void); | |
725 static int ag_action_9_proc(void); | |
726 static int ag_action_10_proc(void); | |
727 static int ag_action_11_proc(void); | |
728 static int ag_action_8_proc(void); | |
729 | |
730 | |
731 static int (*const ag_r_procs_scan[])(void) = { | |
732 ag_action_1_r_proc, | |
733 ag_action_2_r_proc, | |
734 ag_action_3_r_proc, | |
735 ag_action_4_r_proc | |
736 }; | |
737 | |
738 static int (*const ag_s_procs_scan[])(void) = { | |
739 ag_action_1_s_proc, | |
740 ag_action_2_r_proc, | |
741 ag_action_3_s_proc, | |
742 ag_action_4_r_proc | |
743 }; | |
744 | |
745 static int (*const ag_gt_procs_scan[])(void) = { | |
746 ag_action_1_proc, | |
747 ag_action_2_proc, | |
748 ag_action_3_proc, | |
749 ag_action_4_proc, | |
750 ag_action_5_proc, | |
751 ag_action_6_proc, | |
752 ag_action_7_proc, | |
753 ag_action_8_proc, | |
754 ag_action_9_proc, | |
755 ag_action_10_proc, | |
756 ag_action_11_proc, | |
757 ag_action_8_proc | |
758 }; | |
759 | |
760 | |
761 static int ag_action_10_proc(void) { | |
762 (PCB).btsx = 0, (PCB).drt = -1; | |
763 return 0; | |
764 } | |
765 | |
766 static int ag_action_11_proc(void) { | |
767 (PCB).btsx = 0, (PCB).drt = -1; | |
768 (*(token *) &(PCB).vs[(PCB).ssx]) = (PCB).input_value; | |
769 (PCB).ssx--; | |
770 ag_ra(); | |
771 (PCB).ssx++; | |
772 return 0; | |
773 } | |
774 | |
775 static int ag_action_3_r_proc(void) { | |
776 int ag_sd = ag_fl[(PCB).ag_ap] - 1; | |
777 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
778 (PCB).btsx = 0, (PCB).drt = -1; | |
779 (PCB).reduction_token = (ex_token_type) ag_ptt[(PCB).ag_ap]; | |
780 ag_ra(); | |
781 return (PCB).exit_flag == AG_RUNNING_CODE; | |
782 } | |
783 | |
784 static int ag_action_3_s_proc(void) { | |
785 int ag_sd = ag_fl[(PCB).ag_ap] - 1; | |
786 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
787 (PCB).btsx = 0, (PCB).drt = -1; | |
788 (PCB).reduction_token = (ex_token_type) ag_ptt[(PCB).ag_ap]; | |
789 ag_ra(); | |
790 return (PCB).exit_flag == AG_RUNNING_CODE; | |
791 } | |
792 | |
793 static int ag_action_4_r_proc(void) { | |
794 int ag_sd = ag_fl[(PCB).ag_ap] - 1; | |
795 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
796 (PCB).reduction_token = (ex_token_type) ag_ptt[(PCB).ag_ap]; | |
797 return 1; | |
798 } | |
799 | |
800 static int ag_action_2_proc(void) { | |
801 (PCB).btsx = 0, (PCB).drt = -1; | |
802 if ((PCB).ssx >= 128) { | |
803 ag_trace_error(); | |
804 (PCB).exit_flag = AG_STACK_ERROR_CODE; | |
805 PARSER_STACK_OVERFLOW; | |
806 } | |
807 (*(token *) &(PCB).vs[(PCB).ssx]) = (PCB).input_value; | |
808 (PCB).ss[(PCB).ssx] = (PCB).sn; | |
809 (PCB).ssx++; | |
810 (PCB).sn = (PCB).ag_ap; | |
811 return 0; | |
812 } | |
813 | |
814 static int ag_action_9_proc(void) { | |
815 if ((PCB).drt == -1) { | |
816 (PCB).drt=(PCB).token_number; | |
817 (PCB).dssx=(PCB).ssx; | |
818 (PCB).dsn=(PCB).sn; | |
819 } | |
820 ag_prot(); | |
821 (PCB).ss[(PCB).ssx] = (PCB).sn; | |
822 (PCB).ssx++; | |
823 (PCB).sn = (PCB).ag_ap; | |
824 return (PCB).exit_flag == AG_RUNNING_CODE; | |
825 } | |
826 | |
827 static int ag_action_2_r_proc(void) { | |
828 (PCB).ssx++; | |
829 (PCB).sn = (PCB).ag_ap; | |
830 return 0; | |
831 } | |
832 | |
833 static int ag_action_7_proc(void) { | |
834 --(PCB).ssx; | |
835 (PCB).exit_flag = AG_SUCCESS_CODE; | |
836 return 0; | |
837 } | |
838 | |
839 static int ag_action_1_proc(void) { | |
840 (PCB).exit_flag = AG_SUCCESS_CODE; | |
841 return 0; | |
842 } | |
843 | |
844 static int ag_action_1_r_proc(void) { | |
845 (PCB).exit_flag = AG_SUCCESS_CODE; | |
846 return 0; | |
847 } | |
848 | |
849 static int ag_action_1_s_proc(void) { | |
850 (PCB).exit_flag = AG_SUCCESS_CODE; | |
851 return 0; | |
852 } | |
853 | |
854 static int ag_action_4_proc(void) { | |
855 int ag_sd = ag_fl[(PCB).ag_ap] - 1; | |
856 (PCB).reduction_token = (ex_token_type) ag_ptt[(PCB).ag_ap]; | |
857 (PCB).btsx = 0, (PCB).drt = -1; | |
858 (*(token *) &(PCB).vs[(PCB).ssx]) = (PCB).input_value; | |
859 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
860 else (PCB).ss[(PCB).ssx] = (PCB).sn; | |
861 while ((PCB).exit_flag == AG_RUNNING_CODE) { | |
862 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1; | |
863 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1; | |
864 do { | |
865 unsigned ag_tx = (ag_t1 + ag_t2)/2; | |
866 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1; | |
867 else ag_t2 = ag_tx; | |
868 } while (ag_t1 < ag_t2); | |
869 (PCB).ag_ap = ag_pstt[ag_t1]; | |
870 if ((ag_s_procs_scan[ag_astt[ag_t1]])() == 0) break; | |
871 } | |
872 return 0; | |
873 } | |
874 | |
875 static int ag_action_3_proc(void) { | |
876 int ag_sd = ag_fl[(PCB).ag_ap] - 1; | |
877 (PCB).btsx = 0, (PCB).drt = -1; | |
878 (*(token *) &(PCB).vs[(PCB).ssx]) = (PCB).input_value; | |
879 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
880 else (PCB).ss[(PCB).ssx] = (PCB).sn; | |
881 (PCB).reduction_token = (ex_token_type) ag_ptt[(PCB).ag_ap]; | |
882 ag_ra(); | |
883 while ((PCB).exit_flag == AG_RUNNING_CODE) { | |
884 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1; | |
885 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1; | |
886 do { | |
887 unsigned ag_tx = (ag_t1 + ag_t2)/2; | |
888 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1; | |
889 else ag_t2 = ag_tx; | |
890 } while (ag_t1 < ag_t2); | |
891 (PCB).ag_ap = ag_pstt[ag_t1]; | |
892 if ((ag_s_procs_scan[ag_astt[ag_t1]])() == 0) break; | |
893 } | |
894 return 0; | |
895 } | |
896 | |
897 static int ag_action_8_proc(void) { | |
898 ag_undo(); | |
899 ag_trace_error(); | |
900 (PCB).exit_flag = AG_SYNTAX_ERROR_CODE; | |
901 SYNTAX_ERROR; | |
902 | |
903 return (PCB).exit_flag == AG_RUNNING_CODE; | |
904 } | |
905 | |
906 static int ag_action_5_proc(void) { | |
907 int ag_sd = ag_fl[(PCB).ag_ap]; | |
908 (PCB).btsx = 0, (PCB).drt = -1; | |
909 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
910 else { | |
911 (PCB).ss[(PCB).ssx] = (PCB).sn; | |
912 } | |
913 (PCB).reduction_token = (ex_token_type) ag_ptt[(PCB).ag_ap]; | |
914 ag_ra(); | |
915 while ((PCB).exit_flag == AG_RUNNING_CODE) { | |
916 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1; | |
917 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1; | |
918 do { | |
919 unsigned ag_tx = (ag_t1 + ag_t2)/2; | |
920 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1; | |
921 else ag_t2 = ag_tx; | |
922 } while (ag_t1 < ag_t2); | |
923 (PCB).ag_ap = ag_pstt[ag_t1]; | |
924 if ((ag_r_procs_scan[ag_astt[ag_t1]])() == 0) break; | |
925 } | |
926 return (PCB).exit_flag == AG_RUNNING_CODE; | |
927 } | |
928 | |
929 static int ag_action_6_proc(void) { | |
930 int ag_sd = ag_fl[(PCB).ag_ap]; | |
931 (PCB).reduction_token = (ex_token_type) ag_ptt[(PCB).ag_ap]; | |
932 if ((PCB).drt == -1) { | |
933 (PCB).drt=(PCB).token_number; | |
934 (PCB).dssx=(PCB).ssx; | |
935 (PCB).dsn=(PCB).sn; | |
936 } | |
937 if (ag_sd) { | |
938 (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
939 } | |
940 else { | |
941 ag_prot(); | |
942 (PCB).vs[(PCB).ssx] = ag_null_value; | |
943 (PCB).ss[(PCB).ssx] = (PCB).sn; | |
944 } | |
945 while ((PCB).exit_flag == AG_RUNNING_CODE) { | |
946 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1; | |
947 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1; | |
948 do { | |
949 unsigned ag_tx = (ag_t1 + ag_t2)/2; | |
950 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1; | |
951 else ag_t2 = ag_tx; | |
952 } while (ag_t1 < ag_t2); | |
953 (PCB).ag_ap = ag_pstt[ag_t1]; | |
954 if ((ag_r_procs_scan[ag_astt[ag_t1]])() == 0) break; | |
955 } | |
956 return (PCB).exit_flag == AG_RUNNING_CODE; | |
957 } | |
958 | |
959 | |
960 void init_ex(void) { | |
961 unsigned ag_t1; | |
962 ag_t1 = 0; | |
963 (PCB).error_message = "Syntax Error"; | |
964 (PCB).ss[0] = (PCB).sn = (PCB).ssx = 0; | |
965 (PCB).exit_flag = AG_RUNNING_CODE; | |
966 (PCB).btsx = 0, (PCB).drt = -1; | |
967 while (ag_tstt[ag_t1] == 0) { | |
968 (PCB).ag_ap = ag_pstt[ag_t1]; | |
969 (ag_gt_procs_scan[ag_astt[ag_t1]])(); | |
970 ag_t1 = ag_sbt[(PCB).sn]; | |
971 } | |
972 } | |
973 | |
974 void ex(void) { | |
975 (PCB).token_number = (ex_token_type) AG_TCV((PCB).input_code); | |
976 while (1) { | |
977 unsigned ag_t1 = ag_sbt[(PCB).sn]; | |
978 unsigned ag_t2 = ag_sbe[(PCB).sn] - 1; | |
979 do { | |
980 unsigned ag_tx = (ag_t1 + ag_t2)/2; | |
981 if (ag_tstt[ag_tx] > (unsigned char)(PCB).token_number) | |
982 ag_t1 = ag_tx + 1; | |
983 else ag_t2 = ag_tx; | |
984 } while (ag_t1 < ag_t2); | |
985 if (ag_tstt[ag_t1] != (unsigned char)(PCB).token_number) | |
986 ag_t1 = ag_sbe[(PCB).sn]; | |
987 (PCB).ag_ap = ag_pstt[ag_t1]; | |
988 if ((ag_gt_procs_scan[ag_astt[ag_t1]])() == 0) break; | |
989 } | |
990 } | |
991 | |
992 |