shithub: pprolog

ref: a27a5c52f5efeac5165b4dddcb90f207853cc1f5
dir: /misc.c/

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

#include "dat.h"
#include "fns.h"

Term *
appendterm(Term *a, Term *b)
{
	if(a == nil)
		return b;

	Term *tmp;
	for(tmp = a; tmp->next != nil; tmp = tmp->next);
	tmp->next = b;
	return a;
}

int
termslength(Term *list)
{
	int len;
	for(len = 0; list != nil; len++, list = list->next);
	return len;
}

Term *
mkterm(int tag)
{
	Term *t = malloc(sizeof(Term));
	t->tag = tag;
	t->next = nil;
	t->children = nil;
	t->text = nil;
	return t;
}

Term *
mkatom(Rune *name)
{
	Term *t = mkterm(AtomTerm);
	t->text = name;
	return t;
}

Term *
mkvariable(Rune *name)
{
	Term *t = mkterm(VariableTerm);
	t->text = name;
	return t;
}

Term *
mkcompound(Rune *name, int arity, Term *args)
{
	Term *t = mkterm(CompoundTerm);
	t->text = name;
	t->arity = arity;
	t->children = args;
	return t;
}

Term *
mknumber(int type, vlong ival, double dval)
{
	Term *t = mkterm(NumberTerm);
	t->numbertype = type;
	t->ival = ival;
	t->dval = dval;
	return t;
}