shithub: pprolog

ref: 13efe91101a11f41caf6321a8b2fbdd96ef9927a
dir: pprolog/dat.h

View raw version
#define PrecedenceLevels 1200

typedef struct Operator Operator;
typedef struct Term Term;
typedef struct Binding Binding;
typedef struct Goal Goal;
typedef struct Choicepoint Choicepoint;
typedef struct Clause Clause;
typedef struct Predicate Predicate;
typedef struct Module Module;
typedef struct VarName VarName;
typedef int (*Builtin)(Term *, Binding **, Module *);

struct Operator
{
	int type;
	int level;
	Rune *spelling;
	Operator *next;
};

struct Compound
{
	Rune *text;
	int arity;
	Term *children;
};

struct Term
{
	u8int tag;
	u8int inparens;
	Term *next;

	union {
		vlong ival;
		double dval;
		uvlong varnr;
		struct Compound;
	};
};

struct Binding
{
	uvlong varnr;
	Term *value;
	Binding *next;
};

struct Goal
{
	Term *goal;
	uvlong goalnr; /* What clause caused this goal to be activated? */
	Module *module; /* What module is this goal to be evaluated in? */
	Term *catcher; /* When this is non-nil, the goal is a catch frame, goal is the recovery. */
	Goal *next;
};

struct Choicepoint
{
	Goal *goalstack;
	Clause *alternative;
	Binding *altbindings;
	uvlong id; /* Unique number for each clause. Used to know where to cut to. */
	Module *currentmodule;
	Choicepoint *next;
};

struct Clause
{
	Term *head;
	Term *body;
	uvlong clausenr;
	Clause *next;
};

struct Predicate
{
	Rune *name;
	int arity;
	int public;
	int builtin; /* All the predicates from the system module are builtin */
	int dynamic;
	Clause *clauses;
	Predicate *next;
};

struct Module
{
	/* What about imports */
	Rune *name;
	Predicate *predicates;
	Operator *operators[PrecedenceLevels];
	Module *next;
};

struct VarName
{
	Rune *name;
	Term *var;
	int count;
	VarName *next;
};

/* Operator types */
enum {
	Xf	= 1<<0, /* 1 */
	Yf	= 1<<1, /* 2 */
	Xfx	= 1<<2, /* 4 */
	Xfy	= 1<<3, /* 8 */
	Yfx	= 1<<4, /* 16 */
	Fy	= 1<<5, /* 32 */
	Fx	= 1<<6, /* 64 */
};

/* Sorted so that a lower value means it comes earlier in the standard ordering */
enum {
	VariableTerm,
	FloatTerm,
	IntegerTerm,
	AtomTerm,
	CompoundTerm,
};

/* Flags */
enum {
	BoundedTrue,
	BoundedFalse,
};

enum {
	IntegerRoundDown,
	IntegerRoundTowardZero,
};

enum {
	CharConversionOn,
	CharConversionOff,
};

enum {
	DebugOff,
	DebugOn,
};

enum {
	UnknownError,
	UnknownFail,
	UnknownWarning,
};

enum {
	DoubleQuotesChars,
	DoubleQuotesCodes,
	DoubleQuotesAtom,
};

int flagbounded;
vlong flagmaxinteger;
vlong flagmininteger;
int flagintegerroundingfunction;
int flagcharconversion;
int flagdebug;
vlong flagmaxarity;
int flagunknown;
int flagdoublequotes;


/* State of the running system */
Choicepoint *choicestack;
Goal *goalstack;
Module *modules;
uvlong clausenr;
int systemmoduleloaded; /* Is the module "system" ready to be used */