summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Mikkelsen <peter@pmikkelsen.com>2021-06-29 18:46:18 +0000
committerPeter Mikkelsen <peter@pmikkelsen.com>2021-06-29 18:46:18 +0000
commit9799fbd9e8c7fd186365c628bf2024d458dafc75 (patch)
treebc077b6dd46e3922dd1b7ac38631e7039d4cc9bf
parenta27a5c52f5efeac5165b4dddcb90f207853cc1f5 (diff)
Understand :-initialization(Goal) directive
-rw-r--r--dat.h3
-rw-r--r--example.pl5
-rw-r--r--main.c5
-rw-r--r--parser.c10
4 files changed, 18 insertions, 5 deletions
diff --git a/dat.h b/dat.h
index 7c7c0a9..d13ee00 100644
--- a/dat.h
+++ b/dat.h
@@ -25,4 +25,5 @@ enum {
NumberFloat,
};
-int debug; \ No newline at end of file
+int debug;
+Term *initgoals; \ No newline at end of file
diff --git a/example.pl b/example.pl
index 6fff9b3..8f5f0c2 100644
--- a/example.pl
+++ b/example.pl
@@ -16,10 +16,11 @@ likes(sam, ice).
could_be_friends(Person1, Person2) :-
likes(Person1, Thing1),
likes(Person2, Thing2),
- !,
Thing1 = Thing2.
list1(A) :- A = [1,2,3,4].
list2(A) :- A = [a,b|c].
-curly(A) :- A = {one,two,three}. \ No newline at end of file
+curly(A) :- A = {one,two,three}.
+
+:- initialization(could_be_friends(bob, sam)). \ No newline at end of file
diff --git a/main.c b/main.c
index abad53b..10ccec9 100644
--- a/main.c
+++ b/main.c
@@ -33,6 +33,11 @@ main(int argc, char *argv[])
Term *clause;
for(clause = prog; clause != nil; clause = clause->next)
print("%S.\n", prettyprint(clause));
+
+ Term *goal;
+ for(goal = initgoals; goal != nil; goal = goal->next){
+ print("Running query: %S\n", prettyprint(goal));
+ }
}
exits(nil);
diff --git a/parser.c b/parser.c
index df05037..63da254 100644
--- a/parser.c
+++ b/parser.c
@@ -86,6 +86,7 @@ parse(int fd)
print("Could not open file\n");
return nil;
}
+ initgoals = nil;
initoperators();
nexttoken();
@@ -105,8 +106,13 @@ prologtext(void)
syntaxerror("prologtext");
if(t->tag == CompoundTerm && runestrcmp(t->text, L":-") == 0 && t->arity == 1){
- /* A Directive */
- print("Got directive: %S\n", prettyprint(t));
+ Term *body = t->children;
+ print("Got directive: %S\n", prettyprint(body));
+ if(body->tag == CompoundTerm && body->arity == 1 && runestrcmp(body->text, L"initialization") == 0){
+ Term *tmp = initgoals;
+ initgoals = body->children;
+ initgoals->next = tmp;
+ }
t = prologtext();
}else if(t->tag == CompoundTerm && runestrcmp(t->text, L":-") == 0 && t->arity == 2){
t->next = prologtext();