aboutsummaryrefslogtreecommitdiff
path: root/src/id_table.c
blob: 2ad96add467be581a32a40e5ecbb4059d14de8ec (plain) (blame)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "id_table.h"


static IdTableT IdTable[IDTABLE_SIZE];

static int NextAgentId, NextGnameId;

void IdTable_init() {

  int i;
  NextAgentId = START_ID_OF_USER_AGENT -1;
  NextGnameId = START_ID_OF_GNAME -1;
  // 0 : ID_INT(used in expressions)
  // 1 .. START_ID_OF_USER_AGENT-1 : built-in agents
  // START_ID_OF_USER_AGENT .. NUM_AGENTS-1: user defined agents
  // NUM_AGENTS(=ID_NAME) .. : names

  for (i=0; i<=END_ID_OF_AGENT; i++) {
    IdTable[i].name = NULL;
    IdTable[i].aux.arity = -1;   // means ERROR
  }
  for (i=START_ID_OF_GNAME; i<IDTABLE_SIZE; i++) {
    IdTable[i].name = NULL;
    IdTable[i].aux.heap = (VALUE)NULL;
  }

  // built-in agent
  IdTable[ID_TUPLE0].aux.arity = 0;
  IdTable[ID_TUPLE1].aux.arity = 1;
  IdTable[ID_TUPLE2].aux.arity = 2;
  IdTable[ID_TUPLE3].aux.arity = 3;
  IdTable[ID_TUPLE4].aux.arity = 4;
  IdTable[ID_TUPLE5].aux.arity = 5;
  IdTable[ID_NIL].aux.arity = 0;
  IdTable[ID_CONS].aux.arity = 2;
  IdTable[ID_INTAGENT].aux.arity = 1;

  IdTable[ID_APPEND].aux.arity = 2;
  IdTable[ID_ZIP].aux.arity = 2;
  IdTable[ID_ZIPC].aux.arity = 2;
  IdTable[ID_MERGER].aux.arity = 1;
  IdTable[ID_MERGER_P].aux.arity = 1;
  IdTable[ID_ADD].aux.arity = 2;
  IdTable[ID_ADD2].aux.arity = 2;
  IdTable[ID_SUB].aux.arity = 2;
  IdTable[ID_SUB2].aux.arity = 2;
  IdTable[ID_MUL].aux.arity = 2;
  IdTable[ID_MUL2].aux.arity = 2;
  IdTable[ID_DIV].aux.arity = 2;
  IdTable[ID_DIV2].aux.arity = 2;
  IdTable[ID_MOD].aux.arity = 2;
  IdTable[ID_MOD2].aux.arity = 2;
  IdTable[ID_PERCENT].aux.arity = 1;

  IdTable[ID_ERASER].aux.arity = 0;
  IdTable[ID_DUP].aux.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_ZIP].name = "Zip";
  IdTable[ID_ZIPC].name = "ZipC";
  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";
  IdTable[ID_PERCENT].name = "%";

  IdTable[ID_ERASER].name = "Eraser";
  IdTable[ID_DUP].name = "Dup";

}


int IdTable_getid_builtin_funcAgent(Ast *agent) {
  // returns -1 if the agent is not built-in.

  //  puts("!!");
  int id = -1;

  if ((agent->id != AST_AGENT) && (agent->id != AST_PERCENT)) {
    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, "Zip") == 0) {
    id = ID_ZIP;      
  } else if (strcmp((char *)agent->left->sym, "Int") == 0) {
    id = ID_INTAGENT;
  } else if (strcmp((char *)agent->left->sym, "Merger") == 0) {
    id = ID_MERGER;
  } else if (strcmp((char *)agent->left->sym, "Eraser") == 0) {
    id = ID_ERASER;
  } else if (strcmp((char *)agent->left->sym, "Dup") == 0) {
    id = ID_DUP;
  }

  return id;
}



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;
}

char *IdTable_get_name(int id)
{
  return IdTable[id].name;
}


void IdTable_set_arity(int id, int arity)
{
  if ((IdTable[id].aux.arity == -1) || (IdTable[id].aux.arity == arity)) {
    IdTable[id].aux.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].aux.arity, arity);
    IdTable[id].aux.arity = arity;
  } 
}

int IdTable_get_arity(int id)
{
  return IdTable[id].aux.arity;
}


void IdTable_set_heap(int id, VALUE heap)
{
  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].aux.heap = heap;
}

VALUE IdTable_get_heap(int id)
{
  return IdTable[id].aux.heap;
}



int IdTable_new_agentid() {
  NextAgentId++;
  if (NextAgentId > END_ID_OF_USER_AGENT) {
    printf("ERROR: The number of agents exceeded the limitation size (%d).\n",
	 END_ID_OF_USER_AGENT);
    exit(-1);
  }
  return(NextAgentId);
}

int IdTable_new_gnameid() {
  NextGnameId++;
  if (NextGnameId < IDTABLE_SIZE) {
    return(NextGnameId);
  } else {
    
    printf("ERROR: The total number of names exceeded the size of IDTABLE (%d)\n",
	 IDTABLE_SIZE);
    exit(-1);
  }
}