-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathterm.h
More file actions
302 lines (238 loc) · 7.37 KB
/
term.h
File metadata and controls
302 lines (238 loc) · 7.37 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
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 TERM_H_
#define TERM_H_
////////////////////////////////////////
// main data structure
// mimic the recursive nature of TERMs
////////////////////////////////////////
#include <cassert>
#include <cstring>
#include <cstdint>
#include <iostream>
using namespace std;
#include "iafx.h"
#include "operator.h"
class Term;
class Struct;
class List;
class SysData;
class TermArgs;
class TermVisit;
class TermDataVisit;
typedef kstring Atom;
typedef short Int;
typedef double Double;
typedef unsigned Var;
typedef uint64_t TermData;
typedef SysData* SysDataPtr;
typedef Struct* StructPtr;
// port to 64 bit: define pointer size adeguately
#define BUILD_64 1
const TermData
#if BUILD_64
f_VAR = 0x8000000000000000,
f_STRUCT = 0x4000000000000000,
f_INT = 0x2000000000000000,
f_ATOM = 0x1000000000000000,
f_DOUBLE = 0x0800000000000000,
f_LIST = 0x0400000000000000,
f_SYSDATA = 0x0200000000000000,
f_NOTERM = 0x0100000000000000,
MaskFlags = 0xFF00000000000000,
#else
f_VAR = 0x80000000,
f_STRUCT = 0x40000000,
f_INT = 0x20000000,
f_ATOM = 0x10000000,
f_DOUBLE = 0x08000000,
f_LIST = 0x04000000,
f_SYSDATA = 0x02000000,
f_NOTERM = 0x01000000,
MaskFlags = 0xFF000000,
#endif
ListNULL = f_LIST|~MaskFlags;
const int KSTRING_LIST = int(Operator::UserDef);
const int KSTRING_CUT = KSTRING_LIST + 1;
const Var ANONYM_IX = Var(~MaskFlags);
///////////////
// term value
//
class IAFX_API Term {
public:
Term(); // dummy (random init)
Term(TermData); // like a low level copy
Term(Atom); // kstring selector
Term(Int); // short integer
Term(Var); // variable offset
Term(Double); // float number
Term(Atom, int); // struct (add args later)
Term(Term, Term); // list: head and tail
Term(SysData*); // system defined data pointer
Term(CCP, int); // string to list
void NoTerm(); // set to empty or invalid
void Destroy(); // recursive destructor
// accessors: assert if no type OK
operator const List&() const;
operator const Double&() const;
operator Var() const;
operator Int() const;
operator SysData*() const;
operator TermData() const;
// operator kstring() const;
operator CCP() const;
kstring kstr() const;
kstring structData(Term**, int *) const;
TermArgs getargs() const;
TermData type() const; // return type bits
bool type(TermData) const; // check for type bits matching
int is_expr(Operator::opCodeTag, int = 2) const;
int is_query() const; // true if operator '?-'
int is_rule() const; // true if operator ':-'
int is_or() const; // true if operator ';'
int is_and() const; // true if operator ','
int is_command() const; // true if unary operator ':-'
int is_cut() const; // true if atom '!'
int LNULL() const; // true if NULL list (only!)
kstring get_funct() const; // return functor
int get_arity() const; // num of args (0..N)
Term LTerm() const; // left term of expression
Term RTerm() const; // right term of expression
Term getarg(int) const; // return required arg or 0
void setarg(int, Term); // change required argument
static void FreeMem(); // static memory release
#ifdef _DEBUG
static void DumpMem(ostream&, int); // static memory displayer
#endif
vector<TermData*> subterms() const;
string show() const;
private:
TermData m_data;
unsigned ivalue() const; // return the value part
static int useOpDecl; // output flag
/*
// disable 'standard' memory interface
void* operator new(size_t);
void operator delete(void *);
#ifdef _DEBUG
void* operator new(size_t, const char *, int);
#endif
*/
// friend TermDataVisit;
};
//////////////////////////////
// structure
// functor and argument list
// contiguous memory layout
//
class IAFX_API Struct {
private:
kstring funct;
int arity;
// Term args[arity] : a vector of variable dimension
friend class Term;
};
///////////////
// binary list
//
class IAFX_API List {
public:
Term head() const; // head data
Term tail() const; // must point to empty list to terminate
private:
TermData h, t;
friend class Term;
};
//////////////////////////////////////
// fast access to arguments of a Term
// with debug support
//
class IAFX_API TermArgs {
public:
Term getarg(int) const;
private:
Term* args;
#ifdef _DEBUG
int arity;
#endif
friend class Term;
};
//////////////////////////////////////
// system defined data
// derive from this class your data
//
class IAFX_API SysData {
public:
struct rtinfo {
CCP id;
SysData* (*build)();
rtinfo* link;
};
// an identifying key (supposed unique in the system)
SysData(rtinfo&);
// type info access
int istype(CCP) const;
CCP gettype() const;
// enable delete operator on this
virtual int delete_able() const;
// release memory
virtual ~SysData();
// enable copying (should always be true)
virtual int copy_able() const;
// generate a copy of this data
virtual Term copy() const;
// enable 'deep' show
virtual int show_able() const;
// display data
virtual void show(ostream &) const;
// guaranteed a SysData argument: default match names
virtual int unify(Term) const;
// defined only in system
virtual ostream& save(ostream&);
virtual istream& load(istream&);
//private:
// identification (auto register in constructor)
rtinfo& m_key;
static rtinfo *reglist;
friend IAFX_API ostream& operator<<(ostream&, SysData&);
friend IAFX_API istream& operator>>(istream&, SysData*&);
friend class Term;
};
// serial data storage
IAFX_API ostream& operator<<(ostream&, SysData&);
IAFX_API istream& operator>>(istream&, SysData*&);
///////////////////////////////////////////
// exec a preorder visit of requested term
// declare and use a Term stack
//
class TermVisit : protected stack<Term> {
public:
// initialize
TermVisit(Term);
// next visited node
Term next();
// set starting position
void start(Term);
};
class TermDataVisit : protected stack<TermData*> {
public:
// initialize
TermDataVisit(TermData*);
// next visited node
TermData* next();
};
#endif