-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclause.h
More file actions
277 lines (206 loc) · 6.09 KB
/
clause.h
File metadata and controls
277 lines (206 loc) · 6.09 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
IL : Intlog Language
Object Oriented Prolog Project
Copyright (C) 1992-2021 - Ing. Capelli Carlo
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef CLAUSE_H_
#define CLAUSE_H_
//----------------------------------------------------------------
// executable terms, ready for query
// resolve at compile time references to builtins and DB entries
// see DBINTLOG for details
//----------------------------------------------------------------
#include "iafx.h"
#include "term.h"
#include "fastree.h"
// local classes
class Clause;
class CallData;
// external definitions
class IntlogExec;
class DbIntlog;
class DbEntry;
class SrcPosTree;
struct BuiltIn;
#include <iostream>
class IAFX_API Clause {
public:
// init state and check semantic correctness (anonym variables follow named)
Clause(Term tData, kstr_list *vars, DbIntlog *dbOwn, kstring fileId = MSR_NULL, SrcPosTree * = nullptr);
// release allocated memory and term image
~Clause();
// verify construction process
int is_ok() const {
return !get_image().type(f_NOTERM);
}
// qualifiers
int is_rule() const {
return image.is_rule();
}
int is_fact() const {
return !image.is_rule() && !image.is_query() && !image.is_command();
}
int is_query() const {
return image.is_query();
}
int is_command() const {
return image.is_command();
}
// accessors
Term get_image() const {
return image;
}
Term get_head() const {
assert(!is_query());
return image.is_rule()? image.getarg(0) : image;
}
kstring get_source() const {
return src;
}
void set_source(kstring newsrc) {
src = newsrc;
}
TermArgs h_args() const {
assert(!is_query());
return argp;
}
int h_arity() const {
assert(!is_query());
return arity;
}
CallData* get_body() const {
return body;
}
unsigned get_nvars() const {
return n_vars + n_anon;
}
unsigned get_named_vars_count() const {
return n_vars;
}
kstring get_varid(Var) const;
// DB owner (where stored) control
DbIntlog* get_db() const {
return db;
}
void set_db(DbIntlog *owner) {
db = owner;
}
// release local pointer to term
void no_image() {
image = Term(f_NOTERM);
}
// if no variables names, set number
void set_nvars(unsigned nv) {
assert(varids == nullptr);
n_vars = nv;
}
// find term image from calldata pointer
Term cd_image(const CallData*) const;
// file source position storage
SrcPosTree* GetSrcPosTree() const {
return spt;
}
void show() const;
private:
Term image; // parsed term (semantically correct!)
unsigned n_vars; // number of (named) logical variables
kstring* varids; // keep variables names
unsigned n_anon; // number of anonym variables
TermArgs argp; // head arguments
int arity; // head arity
CallData* body; // clause body (0 if fact)
DbIntlog* db; // 'owner' DB
kstring src; // source file (null if asserted)
SrcPosTree* spt; // with optional tree position
// body elements constructor
CallData* makebody(Term *tData, SrcPosTree *pSrc, NodeIndex nIndex);
// check for builtin in table
BuiltIn* builtin(Term) const;
// display error
int err_msg(int, const char* = nullptr) const;
// check for ok functor (identifiable)
int check_funct(Term) const;
};
//-----------------------
// ready to use for call
//
class IAFX_API CallData {
public:
enum cdtype {
DBPRED, // DB stored procedure entry
BUILTIN, // system defined procedure
CUT, // cut (not implemented ancestor cutting)
DISJUNCT, // disjunction
TAILREC // tail recursion optimization
};
// generic accessors
Term val() const {
return owner->cd_image(this);
}
TermArgs args() const {
return argp;
}
// source position
NodeIndex srcpos() const {
return spos;
}
unsigned row() const;
unsigned col() const;
unsigned len() const;
const Clause* get_clause() const {
return owner;
}
enum cdtype get_type() const {
return type;
}
CallData* get_orelse() const {
assert(type == DISJUNCT);
return orelse;
}
BuiltIn* get_builtin() const {
assert(type == BUILTIN);
return bltin;
}
DbEntry* get_dbe() const {
assert(type == DBPRED);
return entry;
}
// return direct right hand brother
CallData* next() const {
return link;
}
// true if last right hand brother
int is_last() const {
return link? 0 : 1;
}
private:
CallData(Clause *c) {
owner = c;
link = nullptr;
spos = INVALID_NODE;
}
~CallData();
TermArgs argp; // fast access to arguments
CallData* link; // right hand brother in clause body
Clause* owner; // back link to fast access body constructor
cdtype type; // union type selector
union {
DbEntry* entry; // if DBPRED
BuiltIn* bltin; // if BUILTIN
CallData* orelse; // if DISJUNCT
};
NodeIndex spos; // keep source position index (default to none)
friend class Clause;
};
#endif