ref: 66a7040df6897e60ee1a513b95f4b04e687bbf0a
parent: 7db38904537603dabe960f32fa505e27db89e27b
author: Peter Mikkelsen <[email protected]>
date: Sat Jul 3 16:59:27 EDT 2021
Add one global choicestack so we don't need to pass it around
--- a/builtins.c
+++ b/builtins.c
@@ -5,7 +5,7 @@
#include "dat.h"
#include "fns.h"
-#define BuiltinProto(name) int name(Term *, Term *, Goal **, Choicepoint **, Binding **)
+#define BuiltinProto(name) int name(Term *, Term *, Goal **, Binding **)
#define Match(X, Y) (runestrcmp(name, X) == 0 && arity == Y)
#define Throw(What) do{\
Goal *g = malloc(sizeof(Goal)); \
@@ -128,21 +128,19 @@
}
int
-builtinfail(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinfail(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goal);
USED(goals);
- USED(choicestack);
USED(bindings);
return 0;
}
int
-builtincall(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtincall(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
- USED(choicestack);
USED(bindings);
Goal *g = malloc(sizeof(Goal));
@@ -155,13 +153,13 @@
}
int
-builtincut(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtincut(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
USED(bindings);
- Choicepoint *cp = *choicestack;
+ Choicepoint *cp = choicestack;
/* Cut all choicepoints with an id larger or equal to the goal clause number, since they must have been introduced
after this goal's parent.
@@ -168,16 +166,15 @@
*/
while(cp != nil && cp->id >= goal->clausenr)
cp = cp->next;
- *choicestack = cp;
+ choicestack = cp;
return 1;
}
int
-builtinvar(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinvar(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
USED(bindings);
Term *arg = goal->children;
return (arg->tag == VariableTerm);
@@ -184,11 +181,10 @@
}
int
-builtinatom(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinatom(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
USED(bindings);
Term *arg = goal->children;
return (arg->tag == AtomTerm);
@@ -195,11 +191,10 @@
}
int
-builtininteger(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtininteger(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
USED(bindings);
Term *arg = goal->children;
return (arg->tag == NumberTerm && arg->numbertype == NumberInt);
@@ -206,11 +201,10 @@
}
int
-builtinfloat(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinfloat(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
USED(bindings);
Term *arg = goal->children;
return (arg->tag == NumberTerm && arg->numbertype == NumberFloat);
@@ -217,11 +211,10 @@
}
int
-builtinatomic(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinatomic(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
USED(bindings);
Term *arg = goal->children;
return (arg->tag == AtomTerm || arg->tag == NumberTerm);
@@ -228,11 +221,10 @@
}
int
-builtincompound(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtincompound(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
USED(bindings);
Term *arg = goal->children;
return (arg->tag == CompoundTerm);
@@ -239,11 +231,10 @@
}
int
-builtinnonvar(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinnonvar(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
USED(bindings);
Term *arg = goal->children;
return (arg->tag != VariableTerm);
@@ -250,11 +241,10 @@
}
int
-builtinnumber(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinnumber(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
USED(bindings);
Term *arg = goal->children;
return (arg->tag == NumberTerm);
@@ -316,11 +306,10 @@
}
int
-builtincompare(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtincompare(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
Term *order = goal->children;
Term *t1 = order->next;
Term *t2 = t1->next;
@@ -339,11 +328,10 @@
}
int
-builtinfunctor(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinfunctor(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
Term *term = goal->children;
Term *name = term->next;
@@ -378,11 +366,10 @@
}
int
-builtinarg(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinarg(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
Term *n = goal->children;
Term *term = n->next;
@@ -416,11 +403,10 @@
}
int
-builtinuniv(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinuniv(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
Term *term = goal->children;
Term *list = term->next;
@@ -491,11 +477,10 @@
}
int
-builtinis(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinis(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
Term *result = goal->children;
Term *expr = result->next;
@@ -508,10 +493,9 @@
}
int
-builtincatch(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtincatch(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
- USED(choicestack);
USED(bindings);
Term *catchgoal = goal->children;
@@ -534,10 +518,9 @@
}
int
-builtinthrow(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinthrow(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
- USED(choicestack);
USED(bindings);
USED(goals);
@@ -564,10 +547,10 @@
*goals = newgoal;
applybinding(newgoal->goal, *bindings);
- Choicepoint *cp = *choicestack;
+ Choicepoint *cp = choicestack;
while(cp != nil && cp->id >= goal->clausenr)
cp = cp->next;
- *choicestack = cp;
+ choicestack = cp;
return 1;
}
}
@@ -576,21 +559,19 @@
}
int
-builtincurrentprologflag(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtincurrentprologflag(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goal);
USED(goals);
- USED(choicestack);
USED(bindings);
return 0;
}
int
-builtinsetprologflag(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinsetprologflag(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
- USED(choicestack);
USED(bindings);
Term *key = goal->children;
Term *value = key->next;
@@ -608,11 +589,10 @@
}
int
-builtinopen(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinopen(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
USED(bindings);
Term *sourcesink = goal->children;
@@ -646,11 +626,10 @@
}
int
-builtinclose(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinclose(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
USED(bindings);
Term *stream = goal->children;
@@ -674,11 +653,10 @@
}
int
-builtincurrentinput(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtincurrentinput(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
USED(bindings);
Term *stream = goal->children;
@@ -690,11 +668,10 @@
}
int
-builtincurrentoutput(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtincurrentoutput(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
USED(bindings);
Term *stream = goal->children;
@@ -706,11 +683,10 @@
}
int
-builtinsetinput(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinsetinput(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
USED(bindings);
Term *stream = goal->children;
@@ -731,11 +707,10 @@
}
int
-builtinsetoutput(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinsetoutput(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
USED(bindings);
Term *stream = goal->children;
@@ -756,11 +731,10 @@
}
int
-builtinreadterm(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinreadterm(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
USED(bindings);
Term *stream = goal->children;
@@ -789,11 +763,10 @@
}
int
-builtinwriteterm(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+builtinwriteterm(Term *database, Term *goal, Goal **goals, Binding **bindings)
{
USED(database);
USED(goals);
- USED(choicestack);
USED(bindings);
Term *stream = goal->children;
--- a/dat.h
+++ b/dat.h
@@ -2,7 +2,7 @@
typedef struct Binding Binding;
typedef struct Goal Goal;
typedef struct Choicepoint Choicepoint;
-typedef int (*Builtin)(Term *, Term *, Goal **, Choicepoint **, Binding **);
+typedef int (*Builtin)(Term *, Term *, Goal **, Binding **);
struct Term
{
@@ -66,3 +66,5 @@
int flagdoublequotes;
+/* Staate of the running system */
+Choicepoint *choicestack;
\ No newline at end of file
--- a/eval.c
+++ b/eval.c
@@ -14,10 +14,9 @@
static uvlong clausenr;
int
-evalquery(Term *database, Term *query, Binding **resultbindings, Choicepoint **resultchoicestack)
+evalquery(Term *database, Term *query, Binding **resultbindings)
{
Goal *goals;
- Choicepoint *choicestack = *resultchoicestack;
if(choicestack == nil){
/*
@@ -66,7 +65,7 @@
/* Try to see if the goal can be solved using a builtin first */
Builtin builtin = findbuiltin(goal);
if(builtin != nil){
- int success = builtin(database, goal, &goals->next, &choicestack, &bindings);
+ int success = builtin(database, goal, &goals->next, &bindings);
if(!success)
goto Backtrack;
}else{
@@ -115,7 +114,6 @@
}
goals = goals->next;
unify(query, goals->goal, resultbindings);
- *resultchoicestack = choicestack;
return 1;
}
--- a/fns.h
+++ b/fns.h
@@ -16,7 +16,7 @@
Term *mklist(Term *);
/* eval.c */
-int evalquery(Term *, Term *, Binding **, Choicepoint **);
+int evalquery(Term *, Term *, Binding **);
int unify(Term *, Term *, Binding **);
void applybinding(Term *, Binding *);
--- a/main.c
+++ b/main.c
@@ -48,8 +48,7 @@
Term *goal;
for(goal = initgoals; goal != nil; goal = goal->next){
Binding *bindings = nil;
- Choicepoint *choicestack = nil;
- evalquery(database, goal, &bindings, &choicestack);
+ evalquery(database, goal, &bindings);
}
}
--- a/repl.c
+++ b/repl.c
@@ -15,10 +15,9 @@
print("?- ");
Term *query = parse(fd, nil, 1);
Binding *bindings = nil;
- Choicepoint *choicestack = nil;
int success;
FindMore:
- success = evalquery(database, query, &bindings, &choicestack);
+ success = evalquery(database, query, &bindings);
if(success == 0)
print("false.\n");
else{