shithub: pprolog

ref: 4fba3e66dce0d167d2031a0d1f1f6f4571cbd981
dir: /types.c/

View raw version
#include <u.h>
#include <libc.h>
#include <bio.h>

#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;	
}

int
ispredicateindicator(Term *t, int allowvars)
{
	if(t->tag == CompoundTerm && runestrcmp(t->text, L"/") == 0 && t->arity == 2){
		Term *f = t->children;
		Term *a = f->next;
		if(allowvars)
			return (f->tag == VariableTerm || f->tag == AtomTerm) && (a->tag == VariableTerm || a->tag == IntegerTerm);
		else
			return (f->tag == AtomTerm) && (a->tag == IntegerTerm);
	}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;
}