shithub: pprolog

Download patch

ref: 9799fbd9e8c7fd186365c628bf2024d458dafc75
parent: a27a5c52f5efeac5165b4dddcb90f207853cc1f5
author: Peter Mikkelsen <peter@pmikkelsen.com>
date: Tue Jun 29 14:46:18 EDT 2021

Understand :-initialization(Goal) directive

--- a/dat.h
+++ b/dat.h
@@ -25,4 +25,5 @@
 	NumberFloat,
 };
 
-int debug;
\ No newline at end of file
+int debug;
+Term *initgoals;
\ No newline at end of file
--- a/example.pl
+++ b/example.pl
@@ -16,10 +16,11 @@
 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
--- a/main.c
+++ b/main.c
@@ -33,6 +33,11 @@
 		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);
--- a/parser.c
+++ b/parser.c
@@ -86,6 +86,7 @@
 		print("Could not open file\n");
 		return nil;
 	}
+	initgoals = nil;
 	initoperators();
 	nexttoken();
 
@@ -105,8 +106,13 @@
 		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();