shithub: pprolog

ref: 50f83a91220940042962fdb55d07bb03991f52be
dir: /builtins.c/

View raw version
#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;
}