#include "structures.h" extern int term_counter; /* these are routines which should be used for constructing the data-structures */ /* creates a var_array of a given length */ var_array *create_vars(int n) { term_pointer *array; var_array *res; array=(term_pointer *)calloc(n, sizeof(term_pointer)); res=(var_array *)malloc(sizeof(var_array)); res->length=n; res->array=array; return res; } void copy_vars(var_array *v1, var_array *v2) { int i; for (i=0;ilength;i++)v2->array[i]=v1->array[i]; } struct clause *copy_clause(struct clause *cl) { struct clause *res; int i; res=(struct clause *)malloc(sizeof(struct clause)); res->head=copy_term(cl->head); res->var_counter=cl->var_counter; res->body_size=cl->body_size; if (res->body_size==0) res->body=NULL; else for (i=0;ibody_size;i++) res->body[i]=copy_term(cl->body[i]); return res; } struct term *offset_copy_term(struct term *t,int offset) { struct term **arg_array; struct term *res; int i; arg_array=maketermarray(t->arity); for (i=0;iarity;i++) arg_array[i]=offset_copy_term(t->argument_array[i],offset); if (t->term_type!=VARIABLE) return maketerm(t->term_type, t->functor, t->arity, t->const_string, arg_array); else return maketerm(t->term_type,t->functor+offset,t->arity, t->const_string,arg_array); } struct clause *offset_copy_clause(struct clause *cl,int offset) { struct clause *res; int i; res=(struct clause *)malloc(sizeof(struct clause)); res->head=offset_copy_term(cl->head,offset); res->body_size=cl->body_size; if (res->body_size==0) { res->body=NULL; } else { res->body=(struct term **)calloc(cl->body_size, sizeof(struct term *)); for (i=0;ibody_size;i++) { res->body[i]=offset_copy_term(cl->body[i], offset); } } return res; } void free_vars(garbage_lists garbage) { var_array *g; struct list *temp; while(garbage.garbage_vars) { g=garbage.garbage_vars->object; free(g->array); free(g); temp=garbage.garbage_vars; garbage.garbage_vars=garbage.garbage_vars->next; free(temp); } } void free_list(struct list *l) { struct list *temp; while (l) { temp=l; l=l->next; free(temp); } } void free_list_cells(garbage_lists ga) { struct list *g; struct list *temp; while(ga.garbage_list_cells) { g=ga.garbage_list_cells->object; free(g); temp=ga.garbage_list_cells; ga.garbage_list_cells=ga.garbage_list_cells->next; free(temp); } } void free_clauses(garbage_lists ga) { struct list *temp; int i; while(ga.garbage_clauses) { free_clause(ga.garbage_clauses->object); temp=ga.garbage_clauses; ga.garbage_clauses=ga.garbage_clauses->next; free(temp); } } void free_terms(garbage_lists ga) { struct list *temp; int i; while(ga.garbage_terms) { free_term(ga.garbage_terms->object); temp=ga.garbage_terms; ga.garbage_terms=ga.garbage_terms->next; free(temp); } } void collect(garbage_lists g) { free_list_cells(g); free_clauses(g); free_vars(g); free_terms(g); } void garbage_init(garbage_lists *g) { g->garbage_vars=NULL; g->garbage_clauses=NULL; g->garbage_list_cells=NULL; g->garbage_terms=NULL; } /* return the list of variable functors in the term in ascending order */ struct list *var_sequence(struct term *t, garbage_lists *g) { int i; struct list *res=NULL; if (t->term_type==VARIABLE) res=g_cons((void *)t->functor,NULL,g); else for(i=0;iarity;i++) res=merge(res,var_sequence(t->argument_array[i],g)); return res; } /* replaces the variables in a term with rationalized variables */ void replace_vars(struct term *t, struct list *var_sequence) { int i; if (t->term_type==VARIABLE) t->functor=position(t->functor,var_sequence); else for(i=0;iarity;i++) replace_vars(t->argument_array[i],var_sequence); } /* ============================================================ | free_clause() | |----------------------------------------------------------| | Params : 1) an interpreter clause | | | | | | Desc : frees the memory occupied by the clause and | | all its constituent parts | | Returns:Nothing | |==========================================================| */ void free_clause(struct clause *cl) { int i; free_term(cl->head); for (i=0;ibody_size;i++) free_term(cl->body[i]); if (cl->body_size!=0) free(cl->body); free(cl); }