summaryrefslogtreecommitdiff
path: root/module.c
diff options
context:
space:
mode:
authorPeter Mikkelsen <peter@pmikkelsen.com>2021-07-16 00:42:49 +0000
committerPeter Mikkelsen <peter@pmikkelsen.com>2021-07-16 00:42:49 +0000
commit1c8789198373a52da9e80dc9b2b1ee2b67af61c4 (patch)
tree980040a8d1828a85428b175eef9f6a4106248309 /module.c
parent2a77288e28f2725b5621c239d2393d49f61993e8 (diff)
Make operators local to each module, and implement some more correct prettyprint code, used by write_term
Diffstat (limited to 'module.c')
-rw-r--r--module.c58
1 files changed, 52 insertions, 6 deletions
diff --git a/module.c b/module.c
index 8e1f043..36effb4 100644
--- a/module.c
+++ b/module.c
@@ -5,8 +5,6 @@
#include "dat.h"
#include "fns.h"
-Module *addemptymodule(Rune *);
-
void
initmodules(void)
{
@@ -33,6 +31,7 @@ parsemodule(char *file)
int fd = open(file, OREAD);
if(fd < 0)
return nil;
+
Term *terms = parse(fd, nil, 0);
if(terms == nil)
@@ -48,11 +47,11 @@ parsemodule(char *file)
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));
+ print("Module name should be an atom in: %S\n", prettyprint(directive, 0, 0, 0, nil));
return nil;
}
- print("Public list for module '%S': %S\n", modulename->text, prettyprint(publiclist, 0, 0, 0));
- m = addemptymodule(modulename->text);
+ print("Public list for module '%S': %S\n", modulename->text, prettyprint(publiclist, 0, 0, 0, nil));
+ m = getmodule(modulename->text);
}
terms = terms->next;
}
@@ -118,11 +117,19 @@ addemptymodule(Rune *name)
Module *m = gmalloc(sizeof(Module));
m->name = name;
m->next = modules;
+ memset(m->operators, 0, sizeof(m->operators));
if(systemmodule == nil)
m->predicates = nil;
- else
+ else{
m->predicates = systemmodule->predicates; /* Direct access to system clauses for now, but when I figure out imports this will change */
+ int level;
+ Operator *op;
+ for(level = 0; level < PrecedenceLevels; level++){
+ for(op = systemmodule->operators[level]; op != nil; op = op->next)
+ addoperator(op->level, op->type, op->spelling, m);
+ }
+ }
modules = m;
return m;
}
@@ -153,4 +160,43 @@ appendpredicate(Predicate *preds, Predicate *new)
tmp->next = new;
return preds;
+}
+
+Operator *
+getoperator(Rune *spelling, Module *mod)
+{
+ Operator *op = nil;
+ int level;
+
+ if(spelling == nil || mod == nil)
+ return nil;
+
+ for(level = 0; level < PrecedenceLevels && op == nil; level++){
+ Operator *tmp;
+ for(tmp = mod->operators[level]; tmp != nil; tmp = tmp->next){
+ if(runestrcmp(tmp->spelling, spelling) == 0){
+ if(op == nil){
+ op = gmalloc(sizeof(Operator));
+ memcpy(op, tmp, sizeof(Operator));
+ }else
+ op->type |= tmp->type;
+ }
+ }
+ }
+ return op;
+}
+
+void
+addoperator(int level, int type, Rune *spelling, Module *mod)
+{
+ if(mod == nil)
+ return;
+
+ /* the operator table is never garbage collected, so just use normal malloc */
+ Operator *op = malloc(sizeof(Operator));
+ op->type = type;
+ op->level = level;
+ op->spelling = spelling;
+ op->next = mod->operators[level-1];
+ mod->operators[level-1] = op;
} \ No newline at end of file