summaryrefslogtreecommitdiff
path: root/builtins.c
diff options
context:
space:
mode:
Diffstat (limited to 'builtins.c')
-rw-r--r--builtins.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/builtins.c b/builtins.c
new file mode 100644
index 0000000..5db5e62
--- /dev/null
+++ b/builtins.c
@@ -0,0 +1,92 @@
+#include <u.h>
+#include <libc.h>
+
+#include "dat.h"
+#include "fns.h"
+
+int builtintrue(Term *, Term *, Goal **, Choicepoint **, Binding **);
+int builtinfail(Term *, Term *, Goal **, Choicepoint **, Binding **);
+int builtincall(Term *, Term *, Goal **, Choicepoint **, Binding **);
+int builtincut(Term *, Term *, Goal **, Choicepoint **, Binding **);
+
+Builtin
+findbuiltin(Term *goal)
+{
+ int arity;
+ Rune *name;
+
+ switch(goal->tag){
+ case AtomTerm:
+ arity = 0;
+ name = goal->text;
+ break;
+ case CompoundTerm:
+ arity = goal->arity;
+ name = goal->text;
+ break;
+ default:
+ return nil;
+ }
+
+ if(!runestrcmp(name, L"true") && arity == 0)
+ return builtintrue;
+ if(!runestrcmp(name, L"fail") && arity == 0)
+ return builtinfail;
+ if(!runestrcmp(name, L"call") && arity == 1)
+ return builtincall;
+ if(!runestrcmp(name, L"!") && arity == 0)
+ return builtincut;
+
+ return nil;
+}
+
+int
+builtintrue(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+{
+ USED(database);
+ USED(goal);
+ USED(goals);
+ USED(choicestack);
+ USED(bindings);
+ return 1;
+}
+
+int
+builtinfail(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, 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)
+{
+ USED(database);
+ USED(choicestack);
+ USED(bindings);
+
+ Goal *g = malloc(sizeof(Goal));
+ g->goal = goal->children;
+ g->next = *goals;
+ *goals = g;
+
+ return 1;
+}
+
+int
+builtincut(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings)
+{
+ USED(database);
+ USED(goals);
+ USED(bindings);
+
+ Choicepoint *cp = *choicestack;
+ while(cp != nil && cp->id == goal->clausenr)
+ cp = cp->next;
+ *choicestack = cp;
+ return 1;
+} \ No newline at end of file