From 48da622d4ad0b4acfe9005dd318ac3f20b4e8672 Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Thu, 22 Jul 2021 21:54:46 +0000 Subject: Big commit changing the way the system is loaded at startup. 1) The loader and system modules are loaded by the C directly into the user module 2) The system module is then loaded with the loader from the user module 3) The loader module is then loaded with the loader from the user module 4) The repl is then loaded with the loader from the loader module 5) The user module is cleared --- module.c | 86 ++++++++++++++++++++++++++-------------------------------------- 1 file changed, 35 insertions(+), 51 deletions(-) (limited to 'module.c') diff --git a/module.c b/module.c index 07786c2..48c2978 100644 --- a/module.c +++ b/module.c @@ -8,55 +8,29 @@ void initmodules(void) { - systemmodule = parsemodule("/sys/lib/prolog/stdlib.pl"); - if(systemmodule == nil){ - print("Can't load /sys/lib/prolog/stdlib.pl\n"); + addemptymodule(L"user"); + if(!addtousermod("/sys/lib/prolog/system.pl")){ + print("Can't load /sys/lib/prolog/system.pl\n"); exits(nil); } - - Predicate *p; - for(p = systemmodule->predicates; p != nil; p = p->next){ - p->builtin = 1; - p->dynamic = 0; + if(!addtousermod("/sys/lib/prolog/loader.pl")){ + print("Can't load /sys/lib/prolog/loader.pl\n"); + exits(nil); } - - usermodule = addemptymodule(L"user"); - parsemodule("/sys/lib/prolog/loader.pl"); } -Module * -parsemodule(char *file) +int +addtousermod(char *file) { - Module *m = nil; - int fd = open(file, OREAD); if(fd < 0) - return nil; + return 0; + Module *usermodule = getmodule(L"user"); Term *terms = parse(fd, nil, 0); if(terms == nil) - return nil; - - /* Actually look at the terms and convert ':-'/2 terms into clauses. - The only directives (terms of type ':-'/1 there should be in the list are - the module specific ones, as the other are handled by parse itself. - */ - if(terms->tag == CompoundTerm && runestrcmp(terms->text, L":-") == 0 && terms->arity == 1){ - Term *directive = terms->children; - if(directive->tag == CompoundTerm && runestrcmp(directive->text, L"module") == 0 && directive->arity == 2){ - Term *modulename = directive->children; - Term *publiclist = modulename->next; - if(modulename->tag != AtomTerm){ - print("Module name should be an atom in: %S\n", prettyprint(directive, 0, 0, 0, nil)); - return nil; - } - if(flagdebug) - print("Public list for module '%S': %S\n", modulename->text, prettyprint(publiclist, 0, 0, 0, nil)); - m = getmodule(modulename->text); - } - terms = terms->next; - } + return 0; Predicate *currentpred = nil; Term *t; @@ -79,10 +53,7 @@ parsemodule(char *file) /* 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); + usermodule->predicates = appendpredicate(currentpred, usermodule->predicates); currentpred = gmalloc(sizeof(Predicate)); currentpred->name = cl->head->text; currentpred->arity = arity; @@ -94,12 +65,8 @@ parsemodule(char *file) }else currentpred->clauses = appendclause(currentpred->clauses, cl); } - if(m) - m->predicates = appendpredicate(currentpred, m->predicates); - else - usermodule->predicates = appendpredicate(currentpred, usermodule->predicates); - - return m; + usermodule->predicates = appendpredicate(currentpred, usermodule->predicates); + return 1; } Module * @@ -119,12 +86,11 @@ addemptymodule(Rune *name) Module *m = gmalloc(sizeof(Module)); m->name = name; m->next = modules; + m->predicates = nil; memset(m->operators, 0, sizeof(m->operators)); - if(systemmodule == nil) - m->predicates = nil; - else{ - m->predicates = systemmodule->predicates; /* Direct access to system clauses for now, but when I figure out imports this will change */ + Module *systemmodule = getmodule(L"system"); + if(systemmodule != nil){ int level; Operator *op; for(level = 0; level < PrecedenceLevels; level++){ @@ -136,6 +102,24 @@ addemptymodule(Rune *name) return m; } +void +removemodule(Rune *name) +{ + Module *m; + Module *prev = nil; + for(m = modules; m != nil; m = m->next){ + if(runestrcmp(m->name, name) != 0) + prev = m; + else{ + if(prev == nil) + modules = m->next; + else + prev->next = m->next; + return; + } + } +} + Clause * appendclause(Clause *clauses, Clause *new) { -- cgit v1.2.3