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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#include "id_table.h"
static IdTableT IdTable[IDTABLE_SIZE];
static int NextAgentId, NextGnameId;
void IdTable_init() {
int i;
NextAgentId = START_ID_OF_AGENT -1;
NextGnameId = START_ID_OF_GNAME -1;
// 0 : ID_INT(used in expressions)
// 1 .. START_ID_OF_AGENT-1 : built-in agents
// START_IS_OF_AGENT .. NUM_AGENTS-1: user defined agents
// NUM_AGENTS(=ID_NAME) .. : names
for (i=0; i<IDTABLE_SIZE; i++) {
IdTable[i].arity = -1;
IdTable[i].name = NULL;
}
// built-in agent
IdTable[ID_TUPLE0].arity = 0;
IdTable[ID_TUPLE1].arity = 1;
IdTable[ID_TUPLE2].arity = 2;
IdTable[ID_TUPLE3].arity = 3;
IdTable[ID_TUPLE4].arity = 4;
IdTable[ID_TUPLE5].arity = 5;
IdTable[ID_NIL].arity = 0;
IdTable[ID_CONS].arity = 2;
IdTable[ID_INTAGENT].arity = 1;
IdTable[ID_APPEND].arity = 2;
IdTable[ID_MERGER].arity = 1;
IdTable[ID_MERGER_P].arity = 1;
IdTable[ID_ADD].arity = 2;
IdTable[ID_ADD2].arity = 2;
IdTable[ID_SUB].arity = 2;
IdTable[ID_SUB2].arity = 2;
IdTable[ID_MUL].arity = 2;
IdTable[ID_MUL2].arity = 2;
IdTable[ID_DIV].arity = 2;
IdTable[ID_DIV2].arity = 2;
IdTable[ID_MOD].arity = 2;
IdTable[ID_MOD2].arity = 2;
IdTable[ID_INT].name = "int";
IdTable[ID_TUPLE0].name = "Tuple0";
IdTable[ID_TUPLE1].name = "Tuple1";
IdTable[ID_TUPLE2].name = "Tuple2";
IdTable[ID_TUPLE3].name = "Tuple3";
IdTable[ID_TUPLE4].name = "Tuple4";
IdTable[ID_TUPLE5].name = "Tuple5";
IdTable[ID_NIL].name = "[]";
IdTable[ID_CONS].name = "Cons";
IdTable[ID_INTAGENT].name = "Int";
IdTable[ID_APPEND].name = "Append";
IdTable[ID_MERGER].name = "Merger";
IdTable[ID_MERGER_P].name = "_MergerP";
IdTable[ID_ADD].name = "Add";
IdTable[ID_ADD2].name = "_Add";
IdTable[ID_SUB].name = "Sub";
IdTable[ID_SUB2].name = "_Sub";
IdTable[ID_MUL].name = "Mul";
IdTable[ID_MUL2].name = "_Mul";
IdTable[ID_DIV].name = "Div";
IdTable[ID_DIV2].name = "_Div";
IdTable[ID_MOD].name = "Mod";
IdTable[ID_MOD2].name = "_Mod";
}
int IdTable_getid_builtin_funcAgent(Ast *agent) {
// returns -1 if the agent is not built-in.
int id = -1;
if (agent->id != AST_AGENT) {
return -1;
}
if (strcmp((char *)agent->left->sym, "Add") == 0) {
id = ID_ADD;
} else if (strcmp((char *)agent->left->sym, "Sub") == 0) {
id = ID_SUB;
} else if (strcmp((char *)agent->left->sym, "Mul") == 0) {
id = ID_MUL;
} else if (strcmp((char *)agent->left->sym, "Div") == 0) {
id = ID_DIV;
} else if (strcmp((char *)agent->left->sym, "Mod") == 0) {
id = ID_MOD;
} else if (strcmp((char *)agent->left->sym, "Append") == 0) {
id = ID_APPEND;
} else if (strcmp((char *)agent->left->sym, "Int") == 0) {
id = ID_INTAGENT;
} else if (strcmp((char *)agent->left->sym, "Merger") == 0) {
id = ID_MERGER;
}
return id;
}
int IdTable_is_builtin_rule(Ast *agentL, Ast *agentR) {
// puts_ast(agentL);
// printf(" ");
// puts_ast(agentR);
// printf("\n");
if ( (agentL->id == AST_TUPLE) && (agentR->id == AST_TUPLE) ) {
// compare these arities
if (agentL->intval == agentR->intval) {
return 1;
}
return 0;
}
int idL = IdTable_getid_builtin_funcAgent(agentL);
if (agentR->id == AST_INT) {
if ((idL == ID_ADD) || (idL == ID_SUB) || (idL == ID_MUL)
|| (idL == ID_DIV) || (idL == ID_MOD)) {
return 1;
}
return 0;
}
if (idL == ID_APPEND) {
if ((agentR->id == AST_CONS) || (agentR->id == AST_NIL)) {
return 1;
}
return 0;
}
if (idL == ID_MERGER) {
if ((agentR->id == AST_TUPLE) && (agentR->intval == 2)) {
// Merger >< Tuple2
return 1;
}
return 0;
}
return 0;
}
void IdTable_set_name(int id, char *symname)
{
if (id > IDTABLE_SIZE) {
printf("Error: The given id %d was beyond of the size of IdTable (%d)\n",
id, IDTABLE_SIZE);
exit(-1);
}
IdTable[id].name = symname;
}
void IdTable_set_arity(int id, int arity)
{
if ((IdTable[id].arity == -1) || (IdTable[id].arity == arity)) {
IdTable[id].arity = arity;
} else {
printf("Warning: The agent '%s' has been already defined as whose arity is %d, but now used as the arity is %d.\n",
IdTable[id].name, IdTable[id].arity, arity);
IdTable[id].arity = arity;
}
}
char *IdTable_get_name(int id)
{
return IdTable[id].name;
}
int IdTable_get_arity(int id)
{
return IdTable[id].arity;
}
int IdTable_new_agentid() {
NextAgentId++;
if (NextAgentId > ID_NAME) {
printf("ERROR: The number of agents exceeded the size of agents in SYMTABLE (%d)\n",
ID_NAME);
exit(-1);
}
return(NextAgentId);
}
int IdTable_new_gnameid() {
NextGnameId++;
if (NextGnameId < IDTABLE_SIZE) {
return(NextGnameId);
} else {
printf("ERROR: The total number of agents and names exceeded the size of names in IDTABLE (%d)\n",
IDTABLE_SIZE);
exit(-1);
}
}
|