From 85adea62d7e8eee9d0e3525d572325db4e58d21a Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Wed, 30 Jun 2021 23:30:13 +0000 Subject: Allow the repl to backtrack to give alternative results --- repl.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) (limited to 'repl.c') diff --git a/repl.c b/repl.c index e81c2f2..2b09596 100644 --- a/repl.c +++ b/repl.c @@ -1,9 +1,12 @@ #include #include +#include #include "dat.h" #include "fns.h" +Rune parsefindmore(int); + void repl(Term *database) { @@ -12,7 +15,10 @@ repl(Term *database) print("?- "); Term *query = parse(fd, 1); Binding *bindings = nil; - int success = evalquery(database, query, &bindings); + Choicepoint *choicestack = nil; + int success; +FindMore: + success = evalquery(database, query, &bindings, &choicestack); if(success == 0) print("false.\n"); else{ @@ -20,10 +26,46 @@ repl(Term *database) print("true.\n"); else{ while(bindings){ - print("%S = %S\n", bindings->name, prettyprint(bindings->value)); + print(" %S = %S%s", + bindings->name, + prettyprint(bindings->value), + bindings->next ? " ,\n" : ""); bindings = bindings->next; } } + if(choicestack != nil){ + print(" "); + if(parsefindmore(fd) == L';'){ + print(";\n"); + goto FindMore; + }else + print(".\n"); + }else{ + print(".\n"); + } } } +} + +Rune +parsefindmore(int fd) +{ + int consctl = open("/dev/consctl", OWRITE); + if(consctl > 0) + write(consctl, "rawon", 5); + else{ + print("Could not open /dev/consctl\n"); + exits("open"); + } + + fd = dup(fd, -1); + Biobuf *input = Bfdopen(fd, OREAD); + Rune peek = Bgetrune(input); + Bterm(input); + + if(consctl > 0){ + write(consctl, "rawoff", 6); + close(consctl); + } + return peek; } \ No newline at end of file -- cgit v1.2.3