comparison anagram/agcore/ut.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 5b21f127e957
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 /*
2 * AnaGram, A System for Syntax Directed Programming
3 * Copyright 1993-2002 Parsifal Software. All Rights Reserved.
4 * See the file COPYING for license and usage terms.
5 *
6 * ut.cpp
7 */
8
9 #include <stdio.h>
10 #include "port.h"
11
12 #include "csexp.h"
13 #include "dict.h"
14 #include "keyword.h"
15 #include "q1glbl.h"
16 #include "rule.h"
17 #include "stacks.h"
18 #include "symbol.h"
19 #include "token.h"
20 #include "tree.h"
21 #include "ut.h"
22
23 //#define INCLUDE_LOGGING
24 #include "log.h"
25
26
27 #define PUREMARK '%'
28
29
30
31 void append_ascii_char(unsigned int j) {
32 switch (j) {
33 case '\a':
34 ass("\\a");
35 break;
36 case '\b':
37 ass("\\b");
38 break;
39 case '\f':
40 ass("\\f");
41 break;
42 case '\n':
43 ass("\\n");
44 break;
45 case '\r':
46 ass("\\r");
47 break;
48 case '\t':
49 ass("\\t");
50 break;
51 case '\v':
52 ass("\\v");
53 break;
54 case '\'':
55 case '\\':
56 case '\"':
57 apprintf("\\%c",j);
58 break;
59 default:
60 if (j >= 32 && j < 127) {
61 acs(j);
62 }
63 else {
64 apprintf("\\%o",j);
65 }
66 break;
67 }
68 }
69
70 void append_char_range(int i, int j) {
71 const char *fmt;
72 if (i == j) {
73 append_char_rep(i);
74 return;
75 }
76 if (j <= 32 || i >= 127) {
77 fmt = "%d..%d";
78 }
79 else if (i > 32 && j < 127) {
80 fmt = "'%c-%c'";
81 }
82 else if (i > 32) {
83 fmt = "'%c'..%d";
84 }
85 else if (j < 127) {
86 fmt = "%d..'%c'";
87 }
88 else {
89 fmt = "%d..%d";
90 }
91 apprintf(fmt, i,j);
92 }
93
94 void append_char_rep(int j) {
95 switch (j) {
96 case '\a':
97 ass("'\\a'");
98 break;
99 case '\b':
100 ass("'\\b'");
101 break;
102 case '\f':
103 ass("'\\f'");
104 break;
105 case '\n':
106 ass("'\\n'");
107 break;
108 case '\r':
109 ass("'\\r'");
110 break;
111 case '\t':
112 ass("'\\t'");
113 break;
114 case '\v':
115 ass("'\\v'");
116 break;
117 case '\'':
118 case '\\':
119 case '\"':
120 apprintf("'\\%c'", j);
121 break;
122 default:
123 if (j > 0 && j <= 26) {
124 apprintf("^%c", j+64);
125 }
126 else if (j >=32 && j < 127) {
127 apprintf("'%c'", j);
128 }
129 else {
130 apprintf("%d", j);
131 }
132 }
133 }
134
135 void append_string_char(int j) {
136 switch (j) {
137 case '\a':
138 ass("\\a");
139 break;
140 case '\b':
141 ass("\\b");
142 break;
143 case '\f':
144 ass("\\f");
145 break;
146 case '\n':
147 ass("\\n");
148 break;
149 case '\r':
150 ass("\\r");
151 break;
152 case '\t':
153 ass("\\t");
154 break;
155 case '\v':
156 ass("\\v");
157 break;
158 case '\\':
159 case '\"':
160 apprintf("\\%c",j);
161 break;
162 default:
163 if (j >= 32 && j < 127) {
164 acs(j);
165 }
166 else {
167 apprintf("\\%o",j);
168 }
169 break;
170 }
171 }
172
173 static void append_vp_form(VpRule vpRule) {
174 int n, i;
175 AgArray<RuleElement> elementList = vpRule->elementList;
176 n = elementList.size();
177 const char *cs = "";
178 for (i = 0; i < n; i++) {
179 ass(cs);
180 atkn(elementList[i].token);
181 cs = ", ";
182 }
183 }
184
185 static void append_vp_forms(int *lb, int n) {
186 const char *cs;
187 int i;
188
189 cs = "";
190 for (i = 0; i < n; i++) {
191 ass(cs);
192 append_vp_form(*lb++);
193 cs = " | ";
194 }
195 }
196
197 AgString proc_name_string(int pn) {
198 LOGSECTION("proc_name_string");
199 LOGV(pn);
200 char buf[100];
201 sprintf(buf, "ag_rp_%d", pn);
202 LOGV(buf);
203 return AgString(buf);
204 }
205
206 void avptkn(int tn) {
207 int vpt, *lb, n, pn;
208
209 lb = dict_str(vp_prod_dict,tn);
210 n = *lb++ - 1;
211 vpt = lb[--n];
212 switch (vpt) {
213 case 1: /* {forms} */
214 acs('{');
215 append_vp_forms(lb,n);
216 acs('}');
217 break;
218 case 2: /* {forms}... */
219 acs('{');
220 append_vp_forms(lb,n);
221 ass("}...");
222 break;
223 case 3: /* [forms] */
224 acs('[');
225 append_vp_forms(lb,n);
226 acs(']');
227 break;
228 case 4: /* [forms]... */
229 acs('[');
230 append_vp_forms(lb,n);
231 ass("]...");
232 break;
233 case 5: /* name? */
234 atkn(*lb);
235 acs('?');
236 break;
237 case 6: /* name?... */
238 atkn(*lb);
239 ass("?...");
240 break;
241 case 7: /* name... */
242 atkn(*lb);
243 ass("...");
244 break;
245 case 8: /* !proc */
246 acs('!');
247 pn = Rule(*lb)->proc_name;
248 ass(proc_name_string(pn).pointer());
249 break;
250 }
251 }
252
253 void atkn(Token token) {
254 Keyword key;
255 int pn;
256 int vptn;
257
258 LOGSECTION("atkn");
259 LOGV(token);
260
261 //Does token have an explicit name, if so, use that.
262 Symbol tokenName = token->token_name;
263 if (tokenName.isNotNull()) {
264 LOGV(tokenName) LCV(tokenName->string.pointer());
265 ass(tokenName->string.pointer());
266 if (token->pure) acs(PUREMARK); return;
267 }
268 // token does not have an explicit name. Is it a keyword?
269 LOGV(token);
270 key = token->key;
271 //if (key) {
272 if (key.isNotNull()) {
273 acs('"');
274 append_key(key);
275 acs('"');
276 if (token->pure) acs(PUREMARK);
277 return;
278 }
279
280 // token isn't a keyword. Does it have a parse tree?
281 LOGV(token);
282 LOGV(token->parse_tree) LCV(ParseTree::count());
283 ParseTree tokenParseTree = token->parse_tree;
284 LOGV(tokenParseTree);
285 LOGV(token);
286 if (tokenParseTree.isNotNull()) {
287 Symbol parseTreeName = tokenParseTree->token_name;
288 LOGV(parseTreeName);
289 if (parseTreeName.isNotNull() && parseTreeName->token_number == token) {
290 LOGV(tokenParseTree) LCV(parseTreeName->string.pointer());
291 tokenName = parseTreeName;
292 ass(tokenName->string.pointer());
293 if (token->pure) acs(PUREMARK);
294 return;
295 }
296 // Parse tree has no name, so use the expression
297 LOGV(token);
298 LOGV(tokenParseTree->expression->asString().pointer());
299 ass(tokenParseTree->expression->asString().pointer());
300 if (token->pure) acs(PUREMARK);
301 return;
302 }
303 // No parse tree. Try virtual production
304 LOGV(token);
305 LOGV(token->vp_prod_number);
306 if ((vptn = token->vp_prod_number) != 0){
307 LOGV(vptn);
308 avptkn(vptn);
309 if (token->pure) acs(PUREMARK);
310 return;
311 }
312 // No virtual production. Is this a partition token?
313 LOGV(token);
314 LOGV(token->part_number);
315 if ((pn = token->part_number) != 0) {
316 LOGV(pn);
317 apprintf("P%03d", pn);
318 if (token->pure) acs(PUREMARK);
319 return;
320 }
321 // Try for immediate action
322 LOGV(token);
323 //LOGV(token->n_expansion_forms);
324 //if (token->n_expansion_forms == 1) {
325 if (token->expansionRuleList.size() == 1) {
326 //unsigned *fl = lstptr(*tp, expansion_forms);
327 //unsigned *fl = token->expansion_forms();
328 LOGV(token);
329 LOGV(token.expansionRule(0));
330 Rule rule = token.expansionRule(0);
331 LOGV(rule);
332 if (rule->immediate_proc) {
333 apprintf("!ag_rp_%d", rule->proc_name);
334 if (token->pure) acs(PUREMARK);
335 return;
336 }
337 }
338 // none of those things. Be satisfied with token number
339 apprintf("T%03d",(int) token);
340 if (token->pure) acs(PUREMARK);
341 }
342
343 AgString token_string(unsigned tn) {
344 ics();
345 atkn(tn);
346 return buildAgString();
347 }
348
349 static void append_item_only(int f, int x) {
350 int n, i;
351 const char *cs;
352
353 Rule rule = f;
354 n = rule->length();
355 cs = "";
356 for (i = 0; i < n; i++) {
357 ass(cs);
358 if (i == x) {
359 ass("< ");
360 }
361 atkn(rule.token(i));
362 //if (i == x) {
363 // acs('þ');
364 //}
365 if (i == x) {
366 acs('>');
367 }
368 cs = ", ";
369 }
370 if (i == x) {
371 if (i > 0) {
372 acs(' ');
373 }
374 #ifdef OLDUI
375 acs('þ');
376 #endif
377 }
378 }
379
380 void append_item(int f, int x) {
381 if (nforms < 1000) {
382 apprintf("R%03d: ", f);
383 }
384 else {
385 apprintf("R%04d: ", f);
386 }
387 append_item_only(f, x);
388 }
389
390 void append_item_brkt(int f, int brkt) {
391 int n, i;
392 const char *cs;
393
394 Rule rule = f;
395 n = rule->length();
396 if (nforms < 1000) {
397 apprintf("R%03d: ", f);
398 }
399 else {
400 apprintf("R%04d: ", f);
401 }
402 cs = "";
403 for (i = 0; i < n; i++) {
404 ass(cs);
405 if (i == brkt) {
406 ass("< ");
407 }
408 atkn(rule.token(i));
409 if (i == brkt) {
410 acs('>');
411 }
412 cs = ", ";
413 }
414 }