From 0c45e33c1b8d094353a5585c44179d1818ff6e1e Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Tue, 6 Jul 2021 17:45:15 +0000 Subject: Group clauses into predicates, and create all valid choicepoints at once. This is wastefull if one branch loops forever, but it is much nicer otherwise, since we know the choicepoints only gets created as long as their head is unifiable with the goal. --- module.c | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) (limited to 'module.c') diff --git a/module.c b/module.c index f3ab01b..f1fb4f2 100644 --- a/module.c +++ b/module.c @@ -7,6 +7,7 @@ Module *addemptymodule(Rune *); Clause *appendclause(Clause *, Clause *); +Predicate *appendpredicate(Predicate *, Predicate *); void initmodules(void) @@ -52,9 +53,11 @@ parsemodule(char *file) terms = terms->next; } + Predicate *currentpred = nil; Term *t; for(t = terms; t != nil; t = t->next){ Clause *cl = malloc(sizeof(Clause)); + int arity; cl->clausenr = 0; cl->public = 1; /* everything is public for now */ cl->next = nil; @@ -65,12 +68,29 @@ parsemodule(char *file) cl->head = t; cl->body = nil; } - - if(m == nil) - usermodule->clauses = appendclause(usermodule->clauses, cl); + if(cl->head->tag == AtomTerm) + arity = 0; else - m->clauses = appendclause(m->clauses, cl); + arity = cl->head->arity; + + /* Figure out if this clause goes into the latest predicate, or if it is the start of a new one */ + if(currentpred == nil || runestrcmp(cl->head->text, currentpred->name) != 0 || arity != currentpred->arity){ + if(m) + m->predicates = appendpredicate(currentpred, m->predicates); + else + usermodule->predicates = appendpredicate(currentpred, usermodule->predicates); + currentpred = malloc(sizeof(Predicate)); + currentpred->name = cl->head->text; + currentpred->arity = arity; + currentpred->clauses = cl; + currentpred->next = nil; + }else + currentpred->clauses = appendclause(currentpred->clauses, cl); } + if(m) + m->predicates = appendpredicate(currentpred, m->predicates); + else + usermodule->predicates = appendpredicate(currentpred, usermodule->predicates); return m; } @@ -94,9 +114,9 @@ addemptymodule(Rune *name) m->next = modules; if(systemmodule == nil) - m->clauses = nil; + m->predicates = nil; else - m->clauses = systemmodule->clauses; /* Direct access to system clauses for now, but when I figure out imports this will change */ + m->predicates = systemmodule->predicates; /* Direct access to system clauses for now, but when I figure out imports this will change */ modules = m; return m; } @@ -113,4 +133,18 @@ appendclause(Clause *clauses, Clause *new) tmp->next = new; return clauses; +} + +Predicate * +appendpredicate(Predicate *preds, Predicate *new) +{ + Predicate *tmp; + + if(preds == nil) + return new; + + for(tmp = preds; tmp->next != nil; tmp = tmp->next); + + tmp->next = new; + return preds; } \ No newline at end of file -- cgit v1.2.3