1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#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 int (*Builtin)(Term *, Binding **, Module *);
struct Operator
{
int type;
int level;
Rune *spelling;
Operator *next;
};
struct Term
{
int tag;
Rune *text;
int arity;
Term *next;
Term *children;
vlong ival;
double dval;
uvlong clausenr;
};
struct Binding
{
Rune *name;
uvlong nr; /* Unique number for each clause. Every time a clause is used, it gets a new number. */
Term *value;
Binding *next;
};
struct Goal
{
Term *goal;
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;
};
/* 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,
};
int debug;
/* Flags */
enum {
DoubleQuotesChars,
DoubleQuotesCodes,
DoubleQuotesAtom,
};
int flagdoublequotes;
/* State of the running system */
Choicepoint *choicestack;
Goal *goalstack;
Module *modules;
Module *systemmodule; /* The module for the builtins. Everything has access to those */
Module *usermodule; /* The default module for user defined predicates */
uvlong clausenr;
|