From 66a7040df6897e60ee1a513b95f4b04e687bbf0a Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Sat, 3 Jul 2021 20:59:27 +0000 Subject: Add one global choicestack so we don't need to pass it around --- builtins.c | 93 ++++++++++++++++++++++---------------------------------------- dat.h | 4 ++- eval.c | 6 ++-- fns.h | 2 +- main.c | 3 +- repl.c | 3 +- 6 files changed, 41 insertions(+), 70 deletions(-) diff --git a/builtins.c b/builtins.c index 0fede28..df5bc33 100644 --- 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 @@ findbuiltin(Term *goal) } 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,106 +153,98 @@ builtincall(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, } 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. */ 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); } 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); } 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); } 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); } 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); } 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); } 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); } 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 @@ compareterms(Term *t1, Term *t2) } 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 @@ builtincompare(Term *database, Term *goal, Goal **goals, Choicepoint **choicesta } 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 @@ builtinfunctor(Term *database, Term *goal, Goal **goals, Choicepoint **choicesta } 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 @@ listlength(Term *term) } 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 @@ aritheval(Term *expr) } 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 @@ builtinis(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, B } 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 @@ builtincatch(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack } 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 @@ builtinthrow(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack *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 @@ builtinthrow(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack } 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 @@ builtinsetprologflag(Term *database, Term *goal, Goal **goals, Choicepoint **cho } 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 @@ builtinopen(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, } 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 @@ builtinclose(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack } 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 @@ builtincurrentinput(Term *database, Term *goal, Goal **goals, Choicepoint **choi } 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 @@ builtincurrentoutput(Term *database, Term *goal, Goal **goals, Choicepoint **cho } 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 @@ builtinsetinput(Term *database, Term *goal, Goal **goals, Choicepoint **choicest } 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 @@ builtinsetoutput(Term *database, Term *goal, Goal **goals, Choicepoint **choices } 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 @@ builtinreadterm(Term *database, Term *goal, Goal **goals, Choicepoint **choicest } 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; diff --git a/dat.h b/dat.h index 9dac8d0..27ed5de 100644 --- a/dat.h +++ b/dat.h @@ -2,7 +2,7 @@ typedef struct Term Term; 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 @@ enum { int flagdoublequotes; +/* Staate of the running system */ +Choicepoint *choicestack; \ No newline at end of file diff --git a/eval.c b/eval.c index 17ee5d1..0fc5f18 100644 --- a/eval.c +++ b/eval.c @@ -14,10 +14,9 @@ Builtin findbuiltin(Term *); 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 @@ Retry: /* 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 @@ Backtrack: } goals = goals->next; unify(query, goals->goal, resultbindings); - *resultchoicestack = choicestack; return 1; } diff --git a/fns.h b/fns.h index 01f4e6a..236b104 100644 --- a/fns.h +++ b/fns.h @@ -16,7 +16,7 @@ Term *mkstring(Rune *); 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 *); diff --git a/main.c b/main.c index fd45838..ed3d7db 100644 --- a/main.c +++ b/main.c @@ -48,8 +48,7 @@ main(int argc, char *argv[]) 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); } } diff --git a/repl.c b/repl.c index 294eebb..bd584eb 100644 --- a/repl.c +++ b/repl.c @@ -15,10 +15,9 @@ repl(Term *database) 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{ -- cgit v1.2.3