shithub: pprolog

Download patch

ref: a106f8db4ec30b159c0fcef120c3e8b55b3ffdb8
parent: afbd56887b77e28f67373e1a3acae36e242fcf50
author: Peter Mikkelsen <peter@pmikkelsen.com>
date: Wed Jun 30 17:38:34 EDT 2021

Add arg/3

--- a/builtins.c
+++ b/builtins.c
@@ -21,6 +21,7 @@
 BuiltinProto(builtinstring);
 BuiltinProto(builtincompare);
 BuiltinProto(builtinfunctor);
+BuiltinProto(builtinarg);
 
 int compareterms(Term *, Term *);
 
@@ -72,6 +73,8 @@
 		return builtincompare;
 	if(Match(L"functor", 3))
 		return builtinfunctor;
+	if(Match(L"arg", 3))
+		return builtinarg;
 
 	return nil;
 }
@@ -336,3 +339,27 @@
 	}
 	return 0;
 }
+
+int
+builtinarg(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+{
+	USED(database);
+	USED(goals);
+	USED(choicestack);
+
+	Term *n = goal->children;
+	Term *term = n->next;
+	Term *arg = term->next;
+
+	if(n->tag != NumberTerm || n->numbertype != NumberInt || n->ival < 0)
+		return 0;
+	if(term->tag != CompoundTerm)
+		return 0;
+	if(n->ival >= term->arity)
+		return 0;
+
+	int i;
+	Term *t;
+	for(i = 0, t = term->children; i < n->ival; i++, t = t->next);
+	return unify(arg, t, bindings);
+}
\ No newline at end of file