From 50f83a91220940042962fdb55d07bb03991f52be Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Wed, 30 Jun 2021 17:03:25 +0000 Subject: Add support for builtins, and implement true/0, fail/0, call/1, and !/0 builtins --- builtins.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 builtins.c (limited to 'builtins.c') 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 +#include + +#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 -- cgit v1.2.3