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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
//----------------------------------------
// for AST
//----------------------------------------
//http://www.hpcs.cs.tsukuba.ac.jp/~msato/lecture-note/comp-lecture/tiny-c-note2.html
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#define MAX_SYMBOLS 256
typedef struct {
char* sym[MAX_SYMBOLS];
int val[MAX_SYMBOLS];
int nth;
} SymbolTable;
SymbolTable SymTable, ConstTable;
void SymTable_init(SymbolTable *table) {
int i;
table->nth = 0;
for (i=0; i<MAX_SYMBOLS; i++) {
table->sym[i] = NULL;
}
}
char *recordSymbol(SymbolTable *table, char *name) {
int i;
char *result;
for (i=0; i< table->nth; i++) {
if (strcmp(table->sym[i], name) == 0) {
// The `name' has existed already,
// and table->sym[i] is used instead of that,
// so starduped memory for the `name' should be freed here.
// free(name);
return table->sym[i];
}
}
// inpla.l で strdup されているので、
// ここでは stardup せずに、単に ポインタを格納するだけ。
table->sym[table->nth]=name;
result = table->sym[table->nth];
table->nth++;
if (table->nth > MAX_SYMBOLS) {
puts("ERROR: The SymbolTable in AST library is too small.");
exit(-1);
}
return result;
}
int lookupEntry(SymbolTable *table, char *name) {
int i;
for (i=0; i< table->nth; i++) {
if (strcmp(table->sym[i], name) == 0) {
return i;
}
}
return -1;
}
void recordVal(SymbolTable *table, char *name, int val) {
int i;
for (i=0; i< table->nth; i++) {
if (strcmp(table->sym[i], name) == 0) {
// The `name' has existed already,
// and table->sym[i] is used instead of that,
// so starduped memory for the `name' should be freed here.
free(name);
table->val[i] = val;
return;
}
}
//table->sym[table->nth]=strdup(name);
table->sym[table->nth]=name;
table->val[table->nth]=val;
table->nth++;
if (table->nth > MAX_SYMBOLS) {
puts("ERROR: The SymbolTable in AST library is too small.");
exit(-1);
}
}
static Ast *AstHeap;
static int NextPtr_AstHeap;
#define MAX_AST_HEAP 10000
void ast_heapInit(void) {
NextPtr_AstHeap = 0;
AstHeap = malloc(sizeof(Ast)*MAX_AST_HEAP);
if (AstHeap == NULL) {
printf("Malloc error [AstHeap]\n");
exit(-1);
}
SymTable_init(&SymTable);
SymTable_init(&ConstTable);
}
void ast_heapReInit(void) {
NextPtr_AstHeap = 0;
}
static Ast *ast_myalloc(void) {
Ast *ptr;
if (NextPtr_AstHeap < MAX_AST_HEAP) {
ptr=&AstHeap[NextPtr_AstHeap];
NextPtr_AstHeap++;
} else {
printf("[Error] All memory for AST was run out.\n");
exit(-1);
}
return ptr;
}
Ast *ast_makeSymbol(char *name) {
Ast *ptr;
ptr = ast_myalloc();
ptr->id = AST_SYM;
ptr->sym = recordSymbol(&SymTable, name);
return ptr;
}
Ast *ast_makeInt(int num) {
Ast *ptr;
ptr = ast_myalloc();
ptr->id = AST_INT;
ptr->intval = num;
return ptr;
}
void ast_recordConst(char *name, int val) {
recordVal(&ConstTable, name, val);
}
Ast *ast_makeAST(AST_ID id, Ast *left, Ast *right) {
Ast *ptr;
if ((id == AST_AGENT) && (right == NULL)) {
int entry = lookupEntry(&ConstTable, left->sym);
if (entry != -1) {
ptr = ast_myalloc();
ptr->id = AST_INT;
ptr->intval = ConstTable.val[entry];
return ptr;
}
}
ptr = ast_myalloc();
ptr->id = id;
ptr->right = right;
ptr->left = left;
return ptr;
}
int count_elem(Ast *p) {
int count=0;
while (p!=NULL) {
p = ast_getTail(p);
count++;
}
return count;
}
Ast *ast_makeTuple(Ast *tuple) {
Ast *ptr;
ptr = ast_myalloc();
ptr->id = AST_TUPLE;
ptr->right = tuple;
ptr->intval = count_elem(tuple);
return ptr;
}
Ast *ast_paramToCons(Ast *ast) {
// ast は List(a, List(b,NULL)) という astlist が入ってくるとして、
// これを AST_CONS(nonsym, a, AST_CONS(nonsym, b, AST_NIL())) と変換する
Ast *at = ast;
while (ast != NULL) {
at->id = AST_CONS;
if (at->right == NULL) {
Ast *ptr;
ptr = ast_myalloc();
ptr->id = AST_NIL;
at->right = ptr;
break;
}
at=at->right;
}
return ast;
}
Ast *ast_addLast(Ast *l, Ast *p)
{
Ast *q;
if(l == NULL) return ast_makeAST(AST_LIST,p,NULL);
q = l;
while(q->right != NULL) q = q->right;
q->right = ast_makeAST(AST_LIST,p,NULL);
return l;
}
Ast *ast_getNth(Ast *p,int nth)
{
if(p->id != AST_LIST){
fprintf(stderr,"bad access to list\n");
exit(1);
}
if(nth > 0) return(ast_getNth(p->right,nth-1));
else return p->left;
}
Ast *ast_getTail(Ast *p)
{
if(p->id != AST_LIST){
fprintf(stderr,"bad access to list\n");
exit(1);
}
else return p->right;
}
void puts_ast(Ast *p) {
static char *string_AstID[] = {
// basic
"SYM", "NAME", "INTNAME", "AGENT", "CNCT", "RULE",
"BODY", "IF", "THEN_ELSE",
// LIST
"LIST",
// annotation
"(*L)", "(*R)",
// extension
"TUPLE", "PPAIR",
"INT", "LD", "ADD", "SUB", "MUL", "DIV", "MOD",
"LT", "LE", "EQ", "NE", "UNM",
"CONS", "NIL", "OPCONS",
"RAND", "SRAND",
// default
"UNDEFINED",
};
if (p==NULL) {printf("NULL"); return;}
switch(p->id) {
case AST_INT:
printf("int %d", p->intval);
break;
case AST_SYM:
printf("%s", p->sym);
break;
case AST_TUPLE:
printf("TUPLE(%d)(", p->intval);
puts_ast(p->right);
printf(")");
break;
case AST_PPAIR:
printf("PPAIR(");
puts_ast(p->right);
printf(")");
break;
case AST_NIL:
printf("NIL");
break;
case AST_ANNOTATION_L:
case AST_ANNOTATION_R:
printf("%s", string_AstID[p->id]);
puts_ast(p->left);
break;
default:
printf("%s(", string_AstID[p->id]);
puts_ast(p->left);
printf(",");
puts_ast(p->right);
printf(")");
}
}
Ast *ast_unfoldABR(Ast *left, char *sym, Ast *paramlist) {
// input:
// left << (AST_AGENT, sym, paramlist)
// output:
// p1,p2,...,pn << (AST_AGENT, sym, params @ [param])
// ==> (AST_CNCT left right)
// where left = (AST_AGENT sym, p1,p2,...,pn,params)
// right = (AST_NAME param)
if (!strcmp(sym, "Append")) {
// paramlist: [a,b] => [b,a]
if (paramlist->right != NULL) {
Ast *tmp = paramlist->left; // tmp for a
paramlist->left = paramlist->right->left; // a := b
paramlist->right->left = tmp; // b := a
}
} else if (!strcmp(sym, "Merger")) {
// left << Merger(paramlist) ==> Merger(left) ~ (paramlist)
Ast *agent_left = ast_makeAST(AST_AGENT, ast_makeSymbol("Merger"), left);
Ast *agent_right = ast_makeTuple(paramlist);
Ast *cnct = ast_makeAST(AST_CNCT, agent_left, agent_right);
return cnct;
}
//puts_ast(left);puts("");
//puts_ast(paramlist);puts("");
Ast *params, *param, *at;
// paramlist: (AST_LIST elem next)
if (paramlist->right == NULL) {
// [x] => [], x
param = paramlist->left;
params = NULL;
} else {
params = param = NULL;
// params@[param] => params, param
at = paramlist;
while (at->right != NULL) {
if (at->right->right == NULL) {
param = at->right->left; // compoment
at->right = NULL; // break the list chain
params = paramlist; // params is the list whose last is just broken
break;
}
at = at->right;
}
}
// puts_ast(params);puts("");
// puts_ast(param);puts("");
// make left as left@params
at = left;
while (at != NULL) {
if (at->right == NULL) {
at->right = params;
break;
}
at = at->right;
}
// puts_ast(left);puts("");
Ast *agent_left = ast_makeAST(AST_AGENT, ast_makeSymbol(sym), left);
Ast *agent_right = param;
Ast *cnct = ast_makeAST(AST_CNCT, agent_left, agent_right);
//puts_ast(agent_left);puts("");
//puts_ast(agent_right);puts("");
//exit(1);
return cnct;
}
Ast *ast_remove_tuple1(Ast *p) {
if (p==NULL) return p;
switch(p->id) {
case AST_INT:
return p;
break;
case AST_SYM:
return p;
break;
case AST_TUPLE:
if (p->intval == 1) {
// (AST_TUPLE NULL (LIST left right))
p = p->right->left;
}
p->right = ast_remove_tuple1(p->right);
return p;
break;
case AST_NIL:
return p;
break;
case AST_ANNOTATION_L:
case AST_ANNOTATION_R:
p->left = ast_remove_tuple1(p->left);
return p;
break;
default:
p->left = ast_remove_tuple1(p->left);
p->right = ast_remove_tuple1(p->right);
return p;
}
}
|