diff options
author | Peter Mikkelsen <peter@pmikkelsen.com> | 2021-06-30 19:33:55 +0000 |
---|---|---|
committer | Peter Mikkelsen <peter@pmikkelsen.com> | 2021-06-30 19:33:55 +0000 |
commit | a8b1fadd149126e9c8d3081a56d206812211f1e6 (patch) | |
tree | 0e841a4c080daa9e6a6697df6630a147bbef3db6 /builtins.c | |
parent | baea4aa939861fd4efbc71b96f93ba890f01ac40 (diff) |
Add builtins for typetests
Diffstat (limited to 'builtins.c')
-rw-r--r-- | builtins.c | 142 |
1 files changed, 136 insertions, 6 deletions
@@ -4,9 +4,21 @@ #include "dat.h" #include "fns.h" -int builtinfail(Term *, Term *, Goal **, Choicepoint **, Binding **); -int builtincall(Term *, Term *, Goal **, Choicepoint **, Binding **); -int builtincut(Term *, Term *, Goal **, Choicepoint **, Binding **); +#define BuiltinProto(name) int name(Term *, Term *, Goal **, Choicepoint **, Binding **) +#define Match(X, Y) (runestrcmp(name, X) == 0 && arity == Y) + +BuiltinProto(builtinfail); +BuiltinProto(builtincall); +BuiltinProto(builtincut); +BuiltinProto(builtinvar); +BuiltinProto(builtinatom); +BuiltinProto(builtininteger); +BuiltinProto(builtinfloat); +BuiltinProto(builtinatomic); +BuiltinProto(builtincompound); +BuiltinProto(builtinnonvar); +BuiltinProto(builtinnumber); +BuiltinProto(builtinstring); Builtin findbuiltin(Term *goal) @@ -27,12 +39,31 @@ findbuiltin(Term *goal) return nil; } - if(!runestrcmp(name, L"fail") && arity == 0) + /* Rewrite this so its not just a long if chain */ + if(Match(L"fail", 0)) return builtinfail; - if(!runestrcmp(name, L"call") && arity == 1) + if(Match(L"call", 1)) return builtincall; - if(!runestrcmp(name, L"!") && arity == 0) + if(Match(L"!", 0)) return builtincut; + if(Match(L"var", 1)) + return builtinvar; + if(Match(L"atom", 1)) + return builtinatom; + if(Match(L"integer", 1)) + return builtininteger; + if(Match(L"float", 1)) + return builtinfloat; + if(Match(L"atomic", 1)) + return builtinatomic; + if(Match(L"compound", 1)) + return builtincompound; + if(Match(L"nonvar", 1)) + return builtinnonvar; + if(Match(L"number", 1)) + return builtinnumber; + if(Match(L"string", 1)) + return builtinstring; return nil; } @@ -79,4 +110,103 @@ builtincut(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, cp = cp->next; *choicestack = cp; return 1; +} + +int +builtinvar(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings) +{ + USED(database); + USED(goals); + USED(choicestack); + USED(bindings); + Term *arg = goal->children; + return (arg->tag == VariableTerm); +} + +int +builtinatom(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings) +{ + USED(database); + USED(goals); + USED(choicestack); + USED(bindings); + Term *arg = goal->children; + return (arg->tag == AtomTerm); +} + +int +builtininteger(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings) +{ + USED(database); + USED(goals); + USED(choicestack); + USED(bindings); + Term *arg = goal->children; + return (arg->tag == NumberTerm && arg->numbertype == NumberInt); +} + +int +builtinfloat(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings) +{ + USED(database); + USED(goals); + USED(choicestack); + USED(bindings); + Term *arg = goal->children; + return (arg->tag == NumberTerm && arg->numbertype == NumberFloat); +} + +int +builtinatomic(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings) +{ + USED(database); + USED(goals); + USED(choicestack); + USED(bindings); + Term *arg = goal->children; + return (arg->tag == AtomTerm || arg->tag == NumberTerm); +} + +int +builtincompound(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings) +{ + USED(database); + USED(goals); + USED(choicestack); + USED(bindings); + Term *arg = goal->children; + return (arg->tag == CompoundTerm); +} + +int +builtinnonvar(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings) +{ + USED(database); + USED(goals); + USED(choicestack); + USED(bindings); + Term *arg = goal->children; + return (arg->tag != VariableTerm); +} + +int +builtinnumber(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings) +{ + USED(database); + USED(goals); + USED(choicestack); + USED(bindings); + Term *arg = goal->children; + return (arg->tag == NumberTerm); +} + +int +builtinstring(Term *database, Term *goal, Goal **goals, Choicepoint **choicestack, Binding **bindings) +{ + USED(database); + USED(goals); + USED(choicestack); + USED(bindings); + Term *arg = goal->children; + return (arg->tag == StringTerm); }
\ No newline at end of file |