diff options
author | Peter Mikkelsen <peter@pmikkelsen.com> | 2021-07-03 21:16:58 +0000 |
---|---|---|
committer | Peter Mikkelsen <peter@pmikkelsen.com> | 2021-07-03 21:16:58 +0000 |
commit | 3f26a0f2a1f699e628136ec5be6178b5ab40fc44 (patch) | |
tree | 5f288b697076387d59111a5731dfdfeec380c447 /eval.c | |
parent | 66a7040df6897e60ee1a513b95f4b04e687bbf0a (diff) |
Make the goalstack global just like the choicestack
Diffstat (limited to 'eval.c')
-rw-r--r-- | eval.c | 46 |
1 files changed, 23 insertions, 23 deletions
@@ -16,8 +16,6 @@ static uvlong clausenr; int evalquery(Term *database, Term *query, Binding **resultbindings) { - Goal *goals; - if(choicestack == nil){ /* The goal stack has the original query at the very bottom, protected by a catch frame where the ->goal field is nil. @@ -25,34 +23,38 @@ evalquery(Term *database, Term *query, Binding **resultbindings) and to get the result we can unify the original query with the one at the bottom of the stack, to get the bindings applied. */ - goals = malloc(sizeof(Goal)); - goals->goal = copyterm(query, nil); - goals->catcher = nil; - goals->next = nil; + goalstack = malloc(sizeof(Goal)); + goalstack->goal = copyterm(query, nil); + goalstack->catcher = nil; + goalstack->next = nil; Goal *protector = malloc(sizeof(Goal)); protector->goal = nil; protector->catcher = mkvariable(L"catch-var"); - protector->next = goals; - goals = protector; + protector->next = goalstack; + goalstack = protector; /* Now add the actual goals */ - goals = addgoals(goals, query); + goalstack = addgoals(goalstack, query); clausenr = 2; /* Start at two since 0 is for the facts in the database, and 1 is for queries */ }else{ goto Backtrack; } - while(goals->goal != nil){ + while(goalstack->goal != nil){ Term *dbstart; Term *goal; + Goal *oldgoalstack; dbstart = database; Retry: - goal = goals->goal; + print("Loop run\n"); + goal = goalstack->goal; + oldgoalstack = goalstack; + goalstack = goalstack->next; - if(goals->catcher){ - goals = goals->next; + if(oldgoalstack->catcher){ + print("Was catchframe\n"); continue; } @@ -61,11 +63,11 @@ Retry: Binding *bindings = nil; Term *clause = nil; - + /* 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, &bindings); + int success = builtin(database, goal, &bindings); if(!success) goto Backtrack; }else{ @@ -75,7 +77,7 @@ Retry: if(clause->next != nil){ /* Add a choicepoint. Note we create a choicepoint every time, so there is room for improvement. */ Choicepoint *cp = malloc(sizeof(Choicepoint)); - cp->goalstack = copygoals(goals); + cp->goalstack = copygoals(oldgoalstack); cp->next = choicestack; cp->retryclause = clause->next; cp->id = clause->clausenr; @@ -90,17 +92,15 @@ Backtrack: Choicepoint *cp = choicestack; choicestack = cp->next; /* freegoals(goals) */ - goals = cp->goalstack; + goalstack = cp->goalstack; dbstart = cp->retryclause; goto Retry; } } - - goals = goals->next; /* Apply bindings to all goals on the stack except catchframes */ Goal *g; - for(g = goals; g != nil; g = g->next){ + for(g = goalstack; g != nil; g = g->next){ if(g->goal != nil && g->catcher == nil) applybinding(g->goal, bindings); } @@ -109,11 +109,11 @@ Backtrack: if(clause != nil && clause->tag == CompoundTerm && clause->arity == 2 && runestrcmp(clause->text, L":-") == 0){ Term *subgoal = copyterm(clause->children->next, nil); applybinding(subgoal, bindings); - goals = addgoals(goals, subgoal); + goalstack = addgoals(goalstack, subgoal); } } - goals = goals->next; - unify(query, goals->goal, resultbindings); + goalstack = goalstack->next; + unify(query, goalstack->goal, resultbindings); return 1; } |