From 347e5bc533070a5e988d82e7588a4e905c7096f3 Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Wed, 30 Jun 2021 14:15:49 +0000 Subject: Give queries another id than clauses, so variable names doesn't clash --- TODO | 1 + eval.c | 7 ++++--- parser.c | 7 ++++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/TODO b/TODO index 4e42205..a9b0bd8 100644 --- a/TODO +++ b/TODO @@ -4,3 +4,4 @@ 4) Stop copying the entire goal stack into every choicepoint 5) Stop creating choicepoints when it is not needed 6) How to implement builtins nicely? +7) Right now we copy and allocate a lot, but almost never free stuff. \ No newline at end of file diff --git a/eval.c b/eval.c index f3a1e15..8a372a3 100644 --- a/eval.c +++ b/eval.c @@ -35,7 +35,7 @@ evalquery(Term *database, Term *query, Binding **resultbindings) Goal *goals; Choicepoint *choicestack = nil; - clausenr = 0; + clausenr = 2; /* Start at two since 0 is for the facts in the database, and 1 is for queries */ /* The goal stack has the original query at the very bottom, protected by a goal there the ->goal field is nil. @@ -128,8 +128,8 @@ findclause(Term *database, Term *goal, Binding **bindings) Term *clause; Term *head; for(; database != nil; database = database->next){ - clausenr++; clause = copyterm(database, &clausenr); + clausenr++; clause->next = database->next; if(clause->tag == CompoundTerm && runestrcmp(clause->text, L":-") == 0 && clause->arity == 2) head = clause->children; @@ -215,9 +215,10 @@ equalterms(Term *a, Term *b) switch(a->tag){ case AtomTerm: - case VariableTerm: case StringTerm: return !runestrcmp(a->text, b->text); + case VariableTerm: + return (runestrcmp(a->text, b->text) == 0 && a->clausenr == b->clausenr); case NumberTerm: if(a->numbertype != b->numbertype) return 0; diff --git a/parser.c b/parser.c index 18573b7..1e4dc41 100644 --- a/parser.c +++ b/parser.c @@ -90,7 +90,12 @@ parse(int fd, int querymode) initoperators(); nexttoken(); - return prologtext(querymode); + Term *result = prologtext(querymode); + if(querymode){ + uvlong id = 1; + result = copyterm(result, &id); + } + return result; } Term * -- cgit v1.2.3