From 0f958749e189e4dacd7a1f70cfc33460e1228d3b Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Wed, 7 Jul 2021 16:32:02 +0000 Subject: Make '=..'/2 work according to spec. Introduce types.c for functions which tells us something about term types. Should be used a lot more instead of explicitly looking into terms->tag everywhere --- types.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 types.c (limited to 'types.c') diff --git a/types.c b/types.c new file mode 100644 index 0000000..6f2b33d --- /dev/null +++ b/types.c @@ -0,0 +1,58 @@ +#include +#include +#include + +#include "dat.h" +#include "fns.h" + +/* Type tests */ +int +islist(Term *t) +{ + return (isemptylist(t) || isnonemptylist(t)); +} + +int +ispartiallist(Term *t) +{ + if(t->tag == VariableTerm) + return 1; + else if(t->tag == CompoundTerm && runestrcmp(t->text, L".") == 0 && t->arity == 2) + return ispartiallist(listtail(t)); + else + return 0; +} + +int +isemptylist(Term *t) +{ + return (t->tag == AtomTerm && runestrcmp(t->text, L"[]") == 0); +} + +int +isnonemptylist(Term *t) +{ + if(t->tag == CompoundTerm && runestrcmp(t->text, L".") == 0 && t->arity == 2) + return islist(listtail(t)); + else + return 0; +} + +/* Other functions */ +Term * +listhead(Term *t) +{ + if(t->tag == CompoundTerm && runestrcmp(t->text, L".") == 0 && t->arity == 2) + return t->children; + else + return nil; +} + +Term * +listtail(Term *t) +{ + if(t->tag == CompoundTerm && runestrcmp(t->text, L".") == 0 && t->arity == 2) + return t->children->next; + else + return nil; +} \ No newline at end of file -- cgit v1.2.3