diff options
Diffstat (limited to 'module.c')
-rw-r--r-- | module.c | 46 |
1 files changed, 40 insertions, 6 deletions
@@ -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 |