diff options
author | Peter Mikkelsen <peter@pmikkelsen.com> | 2021-06-29 18:46:18 +0000 |
---|---|---|
committer | Peter Mikkelsen <peter@pmikkelsen.com> | 2021-06-29 18:46:18 +0000 |
commit | 9799fbd9e8c7fd186365c628bf2024d458dafc75 (patch) | |
tree | bc077b6dd46e3922dd1b7ac38631e7039d4cc9bf | |
parent | a27a5c52f5efeac5165b4dddcb90f207853cc1f5 (diff) |
Understand :-initialization(Goal) directive
-rw-r--r-- | dat.h | 3 | ||||
-rw-r--r-- | example.pl | 5 | ||||
-rw-r--r-- | main.c | 5 | ||||
-rw-r--r-- | parser.c | 10 |
4 files changed, 18 insertions, 5 deletions
@@ -25,4 +25,5 @@ enum { NumberFloat, }; -int debug;
\ No newline at end of file +int debug; +Term *initgoals;
\ No newline at end of file @@ -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 @@ -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); @@ -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(); |