From 6b3075d90d945d5e7e8fd4d28284ffd99dbeb82e Mon Sep 17 00:00:00 2001 From: Shinya Sato Date: Fri, 24 Sep 2021 16:42:54 +0900 Subject: v0.4.1 --- src/id_table.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 src/id_table.c (limited to 'src/id_table.c') diff --git a/src/id_table.c b/src/id_table.c new file mode 100644 index 0000000..b7b267b --- /dev/null +++ b/src/id_table.c @@ -0,0 +1,171 @@ +#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, "Add2") == 0) { + id = ID_ADD2; + } else if (strcmp((char *)agent->left->sym, "Add") == 0) { + id = ID_ADD; + } else if (strcmp((char *)agent->left->sym, "Append") == 0) { + id = ID_APPEND; + } else if (strcmp((char *)agent->left->sym, "Append2") == 0) { + id = ID_APPEND2; + } else if (strcmp((char *)agent->left->sym, "Int") == 0) { + id = ID_INTAGENT; + } + + 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_ADD2)) { + return 1; + } + return 0; + } + + if (idL == ID_APPEND) { + if ((agentR->id == AST_CONS) || (agentR->id == AST_NIL)) { + 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 redefined 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 (NextAgentId > IDTABLE_SIZE) { + printf("ERROR: The number of agents exceeded the size of names in SYMTABLE (%d)\n", + ID_NAME); + exit(-1); + } + return(NextGnameId); +} -- cgit v1.2.3