diff options
author | Peter Mikkelsen <peter@pmikkelsen.com> | 2021-06-30 17:03:25 +0000 |
---|---|---|
committer | Peter Mikkelsen <peter@pmikkelsen.com> | 2021-06-30 17:03:25 +0000 |
commit | 50f83a91220940042962fdb55d07bb03991f52be (patch) | |
tree | c2d1046393d7c3f75becd7b2150afab46156baf0 /builtins.c | |
parent | 347e5bc533070a5e988d82e7588a4e905c7096f3 (diff) |
Add support for builtins, and implement true/0, fail/0, call/1, and !/0 builtins
Diffstat (limited to 'builtins.c')
-rw-r--r-- | builtins.c | 92 |
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 |