-
Notifications
You must be signed in to change notification settings - Fork 3
/
prassert.c
298 lines (261 loc) · 7.22 KB
/
prassert.c
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
/* prassert.c */
/* implementation of assertz, asserta and clause handling
*/
#include <stdio.h>
#include <string.h>
#include "prtypes.h"
#include "prlex.h"
#include "prunify.h"
#include "prmachine.h"
#include "prcnsult.h"
#include "prassert.h"
#define BADCOPYTYPE "Illegal data type in assert"
#define NOTALIST "You did not give me a list"
#define TAILNOTLIST "The tail is not a list"
#define HEADNOTLIST "The head is not a list"
#ifndef SEGMENTED_ACHITECTURE
#define IS_DYNAMIC(X) ((Dyn_mem <= (dyn_ptr_t)(X)) && ((dyn_ptr_t)(X)<HighDyn_ptr))
#else
#define IS_DYNAMIC(X) 1 /* implies you must make a fresh copy of each object */
#endif
extern node_ptr_t DerefNode, NilNodeptr;
extern subst_ptr_t DerefSubst;
extern atom_ptr_t Nil;
extern dyn_ptr_t Dyn_mem, HighDyn_ptr;
static char * VarSubsts[MAX_VAR];
static varindx NVar_copied;
void ini_copy()
{
NVar_copied = 0;
}
/**********************************************************************
copy_clause()
*********************************************************************/
clause_ptr_t copy_clause(int status,node_ptr_t nodeptr,subst_ptr_t substptr,atom_ptr_t* predptr)
{
void copy_node();
clause_ptr_t clauseptr, make_clause();
node_ptr_t clause_head, clause_tail, get_node();
objtype_t type;
ini_copy();
dereference(nodeptr, substptr);
nodeptr = DerefNode;
substptr = DerefSubst;
if(NODEPTR_TYPE(nodeptr) != PAIR)
{
errmsg(NOTALIST);
return(NULL);
}
dereference(NODEPTR_HEAD(nodeptr), substptr);
type = NODEPTR_TYPE(DerefNode);
if(type != PAIR)
{
if(type == ATOM)
{
clause_head = get_node(status);
copy_node(status, clause_head, nodeptr, substptr);
clause_tail = NilNodeptr;
clauseptr = make_clause(clause_head, clause_tail, status, predptr);
return(clauseptr);
}
else
return(NULL);
}
else
clause_head = get_node(status);
copy_node(status, clause_head, DerefNode, DerefSubst);
dereference(NODEPTR_TAIL(nodeptr), substptr);
if(IS_NIL(DerefNode))
{
clause_tail = NilNodeptr;
}
else
if(NODEPTR_TYPE(DerefNode) != PAIR)
{
errmsg(TAILNOTLIST);
return(NULL);
}
else
{
clause_tail = get_node(status);
copy_node(status, clause_tail, DerefNode, DerefSubst);
}
clauseptr = make_clause(clause_head, clause_tail, status, predptr);
return(clauseptr);
}
/*********************************************************************
copy_node()
**********************************************************************/
void copy_node(int status,node_ptr_t target,node_ptr_t source, subst_ptr_t substptr)
{
objtype_t type;
string_ptr_t stringptr, s, get_string();
#ifdef REAL
real_ptr_t realptr, get_real();
#endif
pair_ptr_t pairptr, get_pair();
char *molec;
int i;
type = NODEPTR_TYPE(source);
NODEPTR_TYPE(target) = type;
switch(type)
{
case ATOM:
NODEPTR_ATOM(target) = NODEPTR_ATOM(source);
break;
case VAR:
molec = NODEPTR_OFFSET(source) + (char *)substptr;
for(i = 0; i < NVar_copied; i++)/*search molec in Varsubsts */
{
if(molec == VarSubsts[i])break;
}
if(i == NVar_copied)/* it's new */
{
VarSubsts[i] = molec;
NVar_copied++;
}
NODEPTR_OFFSET(target) = i * sizeof(struct subst);
break;
case INT:
NODEPTR_INT(target) = NODEPTR_INT(source);
break;
#ifdef CHARACTER
case CHARACTER:
NODEPTR_CHARACTER(target) = NODEPTR_CHARACTER(source);
break;
#endif
#ifdef REAL
case REAL:
if(IS_DYNAMIC(NODEPTR_REALP(source)))
{
realptr = get_real(status);
*realptr = NODEPTR_REAL(source);
NODEPTR_REALP(target) = realptr;
}
else
{
NODEPTR_REALP(target) = NODEPTR_REALP(source);
}
break;
#endif
case PAIR:
pairptr = get_pair(status);
NODEPTR_PAIR(target) = pairptr;
dereference(NODEPTR_HEAD(source), substptr);
copy_node(status, NODEPTR_HEAD(target), DerefNode, DerefSubst);
dereference(NODEPTR_TAIL(source), substptr);
copy_node(status, NODEPTR_TAIL(target), DerefNode, DerefSubst);
break;
case STRING:
s = NODEPTR_STRING(source);
if(IS_DYNAMIC(s))
{
if(status == PERMANENT)status = PERM_STRING;
stringptr = get_string((my_alloc_size_t)(strlen(s) + 1), status);
strcpy(stringptr, s);
NODEPTR_STRING(target) = stringptr;
}
else
NODEPTR_STRING(target) = s;
break;
default:
errmsg(BADCOPYTYPE);
}
}
/*********************************************************************
do_assertz()
For the assertz builtin.
*********************************************************************/
int do_assertz(int status, node_ptr_t nodeptr, subst_ptr_t substptr)
{
clause_ptr_t clauseptr;
atom_ptr_t pred;
clauseptr = copy_clause(status, nodeptr, substptr, &pred);
if(clauseptr == NULL)
return(0);
add_to_end(clauseptr, pred);
return(1);
}
/*********************************************************************
do_asserta()
For the asserta builtin.
*********************************************************************/
int do_asserta(int status, node_ptr_t nodeptr, subst_ptr_t substptr)
{
clause_ptr_t clauseptr;
atom_ptr_t pred;
clauseptr = copy_clause(status, nodeptr, substptr, &pred);
if(clauseptr == NULL)
return(0);
record_pred(pred);
CLAUSEPTR_NEXT(clauseptr) = ATOMPTR_CLAUSE(pred);
ATOMPTR_CLAUSE(pred) = clauseptr;
return(1);
}
/******************************************************************************
do_assertn()
asserts a clause at the nth position if it can
n begins at 1.
******************************************************************************/
int do_assertn(int status, node_ptr_t nodeptr, subst_ptr_t substptr, integer n)
{
clause_ptr_t clauseptr;
atom_ptr_t pred;
if (n < 1)
return 0;
if (n == 1)
return(do_asserta(status, nodeptr, substptr));
clauseptr = copy_clause(status, nodeptr, substptr, &pred);
if (clauseptr == NULL)
return 0;
return (add_as_nth(clauseptr, pred, n));
}
/******************************************************************************
remove_clause()
******************************************************************************/
int remove_clause(atom_ptr_t atomptr,/* the predicate */ clause_ptr_t clauseptr/* remove this */ )
{
if (clauseptr == ATOMPTR_CLAUSE(atomptr))
{
ATOMPTR_CLAUSE(atomptr) = CLAUSEPTR_NEXT(clauseptr);
return 1;
}
else
{
clause_ptr_t clp, previous;
clp = ATOMPTR_CLAUSE(atomptr);
for(;;)
{
previous = clp;
clp = CLAUSEPTR_NEXT(clp);
if(clp == NULL)
return 0;
if (clp == clauseptr)
{
CLAUSEPTR_NEXT(previous) = CLAUSEPTR_NEXT(clp);
return 1;
}
}
}
}
/******************************************************************************
add_as_nth()
Tries to add a clause in the nth position of its packet.
Returns 0 iff unsuccessful.
******************************************************************************/
int add_as_nth(clause_ptr_t clauseptr,atom_ptr_t pred, integer n)
{
clause_ptr_t clp;
clp = ATOMPTR_CLAUSE(pred);
--n;
while (--n >= 0)
{
clp = CLAUSEPTR_NEXT( clp);
if( clp == NULL)
return 0;
}
CLAUSEPTR_NEXT( clauseptr) = CLAUSEPTR_NEXT( clp );
CLAUSEPTR_NEXT( clp) = clauseptr;
return 1;
}
/* end of file */