#include #include #include #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; iid != 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 (%d) for agents in IDTABLE\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 names exceeded the size of IDTABLE (%d)\n", IDTABLE_SIZE); exit(-1); } }