/*********************************************************** * CONVERT * ************************************************************ * Description: * * * * Created by: avrami tzur * * At: Wed Jul 29 09:57:36 1992 * ***********************************************************/ /* INCLUDES */ # include "parser.h" /* DECLARATIONS */ module_ptr convert_structure(parser_clause_list *ptr, char *agent, int sym_count, functor **sym_table); clause_ptr convert_clause(parser_clause *ptr); void update_forward_table(module_ptr mod, struct clause *cl); void update_backward_table(module_ptr mod, struct clause *cl); void update_direct_table(module_ptr mod, struct clause *cl); void update_direct_forward_table(module_ptr mod, struct clause *cl); static term_ptr convert_atom(parser_term *ptr); static term_ptr convert_term(parser_term *ptr); static int atom_type(parser_term *ptr); SystemActionDescriptor *GetSysActionDescriptor(int functor); static void initialize_tables(module_ptr module,int count); void *my_alloc(int size); /* ============================================================ | initilize_tables() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : | | | | Returns: | |==========================================================| */ static void initialize_tables(module_ptr module,int count) { int i; /* Alocate the memory. */ module->prop_table= (struct list **)my_alloc(count*sizeof(struct list *)); module->clause_head_table= (struct list **)my_alloc(count*sizeof(struct list *)); module->clause_body_table= (struct list **)my_alloc(count*sizeof(struct list *)); module->clause_direct_table= (struct list **)my_alloc(count*sizeof(struct list *)); module->direct_forward_table= (struct list **)my_alloc(count*sizeof(struct list *)); module->encountered= (struct list **)my_alloc(count*sizeof(struct list *)); /* Initilize the tables. */ for (i=0;i< count;i++){ module->prop_table[i]=NULL; module->clause_head_table[i]=NULL; module->clause_body_table[i]=NULL; module->clause_direct_table[i]=NULL; module->encountered[i]=NULL; } } void update_forward_table(module_ptr mod, struct clause *cl) { int head_functor=cl->head->functor; struct indexed_clause *i_clause; int body_functor,j; for(j=0;jbody_size;j++) { i_clause=(struct indexed_clause *) my_alloc(sizeof(struct indexed_clause)); if (i_clause==NULL) { printf("no memory for indexed_clause\n"); exit(0); } i_clause->position=j; i_clause->cl=cl; body_functor=cl->body[j]->functor; mod->clause_body_table[body_functor]= append(mod->clause_body_table[body_functor], cons(i_clause,NULL)); } } void update_direct_forward_table(module_ptr mod, struct clause *cl) { int head_functor=cl->head->functor; struct indexed_clause *i_clause; int body_functor,j; for(j=0;jbody_size;j++) { i_clause=(struct indexed_clause *) my_alloc(sizeof(struct indexed_clause)); if (i_clause==NULL) { printf("no memory for indexed_clause\n"); exit(0); } i_clause->position=j; i_clause->cl=cl; body_functor=cl->body[j]->functor; mod->direct_forward_table[body_functor]= append(mod->direct_forward_table[body_functor], cons(i_clause,NULL)); } } void update_backward_table(module_ptr mod, struct clause *cl) { int head_functor=cl->head->functor; int body_functor,j; mod->clause_head_table[head_functor]= append(mod->clause_head_table[head_functor],cons(cl,NULL)); } void update_direct_table(module_ptr mod, struct clause *cl) { int head_functor=cl->head->functor; int body_functor,j; if (sym_table[cl->head->functor]->type!='S' && sym_table[cl->head->functor]->type!='R') { mod->clause_direct_table[head_functor]= append(mod->clause_direct_table[head_functor],cons(cl,NULL)); sym_table[cl->head->functor]->type='D'; } } /* ============================================================ | convert_structure() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : Convert the parsing presentation to the | | interpreter structures. | | | | Returns: | |==========================================================| */ module_ptr convert_structure(parser_clause_list *ptr, char *agent, int sym_count, functor **sym_table) { module_ptr module = (module_ptr)my_alloc(sizeof(struct module_str)); int i; struct clause *cl; parser_clause_list *temp; int agent_functor=functor_string(agent); module->type=A_MOD; initialize_tables(module,sym_count); /* Store agent name */ strcpy(module->name, agent); sym_table[agent_functor]->agent_ref=module; module->agent_functor=agent_functor; /* Count the number of clauses */ module->clause_counter = 0; temp = ptr; while(temp != NULL){ module->clause_counter++; temp = temp->next; } /* Create the clauses array */ module->clause_array = (clause_ptr *)my_alloc(module->clause_counter * sizeof(clause_ptr)); /* Convert the clauses */ temp = ptr; for(i=0; i< module->clause_counter; i++){ cl=convert_clause(temp->ptr); module->clause_array[i] = cl; switch (temp->ptr->clause_type) { case DIRECT_CLAUSE: update_direct_table(module,cl); break; case FORWARD_CLAUSE: update_forward_table(module,cl); break; case DIRECT_FORWARD_CLAUSE: update_direct_forward_table(module,cl); break; case BACKWARD_CLAUSE: update_backward_table(module,cl); break; } if (cl->body_size==0 && sym_table[cl->head->functor]->type=='R') update_backward_table(module,cl); temp = temp->next; } /* Return the module */ return(module); } extern int list_clauses; /* ============================================================ | convert_clause() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc :Convert a clause. | | | | Returns: | |==========================================================| */ clause_ptr convert_clause(parser_clause *ptr) { clause_ptr cl = (clause_ptr)my_alloc(sizeof(struct clause)); parser_term_list *temp; int i, j; term_ptr temp_atom; /* Convert the head atom */ cl->head = convert_atom(ptr->head); /* Assign the number of distinct vars. */ cl->var_counter = ptr->var_counter; /* Count the number of atoms */ cl->body_size = 0; temp = ptr->body; while(temp != NULL){ cl->body_size++; temp = temp->next; } /* Create the body array*/ cl->body = (term_ptr *)my_alloc(cl->body_size * sizeof(term_ptr)); /* Convert the body and direct atoms */ temp = ptr->body; j=0; while(temp){ temp_atom=convert_atom(temp->ptr); cl->body[j]=temp_atom; j++; temp = temp->next; } /* Return the clause */ if (list_clauses) { print_clause(cl,sym_table); } return(cl); } /* ============================================================ | convert_atom() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : Convert an atom | | | | Returns: | |==========================================================| */ static term_ptr convert_atom(parser_term *ptr) { if (GetSysActionDescriptor(ptr->index)!=NULL) sym_table[ptr->index]->action_type= ACTION; else sym_table[ptr->index]->action_type=NON_ACTION; /* Convert the term */ return convert_term(ptr); } /* ============================================================ | convert_term() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : Convert a term. | | | | Returns: | |==========================================================| */ static term_ptr convert_term(parser_term *ptr) { term_ptr te = (term_ptr)my_alloc(sizeof(struct term)); parser_term_list *temp; int i; /* Set term type ,index/const and referance number.*/ te->term_type = ptr->type; te->functor = ptr->index; /* te->local_function_num = sym_table[ptr->index]->local_function_num;*/ if(te->term_type == STRING) { te->const_string = (char *)my_alloc(strlen(ptr->const_string)+1); strcpy(te->const_string, ptr->const_string); }else te->const_string = NULL; /* Convert the arguments array*/ if((te->term_type == NONVAR) || (te->term_type == TUPLE)){ /* Count the number of arguments */ te->arity = 0; temp = ptr->arguments; while(temp != NULL){ te->arity++; temp = temp->next; } /* Create the arguments array*/ te->argument_array = (term_ptr *)my_alloc(te->arity*sizeof(term_ptr)); /* Convert the arguments */ temp = ptr->arguments; for(i=0; iarity; i++){ te->argument_array[i] = convert_term(temp->ptr); temp = temp->next; } } else{ te->argument_array = NULL; te->arity = 0; } /* Return the term */ return(te); } /* ============================================================ | reserved_word() | |----------------------------------------------------------| | Params : 1) | | 2) | | 3) | | Desc : returns the special code associated with the | | predicate name . | | | | Returns: | |==========================================================| */ /*int reserved_word(char *name) { int n=sizeof(reserved_names)/sizeof(char *); int i=1; while(i