shithub: pprolog

Download patch

ref: e6a38aa97b9de05ae0eede568212667b68784a60
parent: 0f958749e189e4dacd7a1f70cfc33460e1228d3b
author: Peter Mikkelsen <peter@pmikkelsen.com>
date: Wed Jul 7 12:44:25 EDT 2021

Add copy_term/2

--- a/builtins.c
+++ b/builtins.c
@@ -32,6 +32,7 @@
 BuiltinProto(builtinfunctor);
 BuiltinProto(builtinarg);
 BuiltinProto(builtinuniv);
+BuiltinProto(builtincopyterm);
 BuiltinProto(builtinis);
 BuiltinProto(builtincatch);
 BuiltinProto(builtinthrow);
@@ -98,6 +99,8 @@
 		return builtinarg;
 	if(Match(L"=..", 2))
 		return builtinuniv;
+	if(Match(L"copy_term", 2))
+		return builtincopyterm;
 	if(Match(L"is", 2))
 		return builtinis;
 	if(Match(L"catch", 3))
@@ -459,6 +462,17 @@
 		Term *reallist = mkcompound(L".", 2, t);
 		return unify(list, reallist, bindings);
 	}
+}
+
+int
+builtincopyterm(Term *goal, Binding **bindings, Module *module)
+{
+	USED(module);
+	Term *term1 = goal->children;
+	Term *term2 = term1->next;
+	Term *t = copyterm(term1, &clausenr);
+	clausenr++;
+	return unify(term2, t, bindings);
 }
 
 #define ToFloat(t) (t->tag == IntegerTerm ? (double)t->ival : t->dval)
--- a/dat.h
+++ b/dat.h
@@ -97,3 +97,4 @@
 Module *modules;
 Module *systemmodule; /* The module for the builtins. Everything has access to those */
 Module *usermodule; /* The default module for user defined predicates */
+uvlong clausenr;
\ No newline at end of file
--- a/eval.c
+++ b/eval.c
@@ -12,8 +12,6 @@
 Builtin findbuiltin(Term *);
 void addchoicepoints(Clause *, Term *, Goal *, Module *);
 
-static uvlong clausenr;
-
 int
 evalquery(Term *query, Binding **resultbindings)
 {
@@ -38,8 +36,6 @@
 
 		/* Now add the actual goals */
 		goalstack = addgoals(goalstack, query, usermodule);
-
-		clausenr = 2; /* Start at two since 0 is for the facts in the database, and 1 is for queries */
 
 	}else{
 		goto Backtrack;
--- a/main.c
+++ b/main.c
@@ -18,6 +18,7 @@
 		usage();
 	}ARGEND
 
+	clausenr = 2; /* Start at two since 0 is for the facts in the database, and 1 is for queries */
 	initflags();
 	initstreams();
 	initmodules();