summaryrefslogtreecommitdiff
path: root/repl.c
diff options
context:
space:
mode:
authorPeter Mikkelsen <peter@pmikkelsen.com>2021-06-30 23:30:13 +0000
committerPeter Mikkelsen <peter@pmikkelsen.com>2021-06-30 23:30:13 +0000
commit85adea62d7e8eee9d0e3525d572325db4e58d21a (patch)
treee976789abf464f9868f9a12778285329db785d7a /repl.c
parentfa83d3f1aba932e99833244ebb38b7415b142bd7 (diff)
Allow the repl to backtrack to give alternative results
Diffstat (limited to 'repl.c')
-rw-r--r--repl.c46
1 files changed, 44 insertions, 2 deletions
diff --git a/repl.c b/repl.c
index e81c2f2..2b09596 100644
--- a/repl.c
+++ b/repl.c
@@ -1,9 +1,12 @@
#include <u.h>
#include <libc.h>
+#include <bio.h>
#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