-
Notifications
You must be signed in to change notification settings - Fork 3
/
prcnsult.c
224 lines (196 loc) · 5.24 KB
/
prcnsult.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
/* prcnsult.c */
/* load a file of clauses */
#include <stdio.h>
#include <string.h>
#include "prtypes.h"
#include "prmachine.h"
#include "prprint.h"
#define CANTLOAD "Can't load %s"
#define ERRBUILTIN "Can't redefine builtin %s"
#define HEADNOTATOM "Predicate not atom!"
extern char *Print_buffer;
extern varindx Nvars;
extern FILE *Curr_infile;
extern atom_ptr_t LastBuiltin;
extern int Trace_flag;
void add_to_end();
void record_pred();
void do_listing();
unsigned int Inp_linecount;
pred_rec_ptr_t First_pred, Last_pred;
static int Pred_count = 0;
static char Filename[80];
/******************************************************************
make_clause()
******************************************************************/
//clause_ptr_t make_clause(clhead, clgoals, status, predptr)
//node_ptr_t clhead, clgoals;
//atom_ptr_t *predptr;
clause_ptr_t make_clause(node_ptr_t clhead, node_ptr_t clgoals, int status,atom_ptr_t* predptr)
{
clause_ptr_t clauseptr, get_clause();
ENTER("make_clause");
clauseptr = get_clause(status);
CLAUSEPTR_GOALS(clauseptr) = clgoals;
CLAUSEPTR_HEAD(clauseptr) = clhead;
CLAUSEPTR_NVARS(clauseptr) = Nvars * sizeof(struct subst);
CLAUSEPTR_NEXT(clauseptr) = NULL;
if(NODEPTR_TYPE(clhead) == PAIR)
{
clhead = NODEPTR_HEAD(clhead);
if(NODEPTR_TYPE(clhead) != ATOM)
{
errmsg(HEADNOTATOM);
return(NULL);/* could also indicate which one !! */
}
*predptr = NODEPTR_ATOM(clhead);
}
return(clauseptr);
}
/******************************************************************
do_consult()
******************************************************************/
int do_consult(char *infilename,int status)
{
extern node_ptr_t NilNodeptr;
extern FILE *Curr_outfile;
static int first = 1;
FILE *ifp, *save_cif, *save_cof;
atom_ptr_t the_pred;
clause_ptr_t clauseptr;
node_ptr_t the_list, the_head, read_list();
// char buffer[4096];
ENTER("do_consult");
Inp_linecount = 0;
if (first == 1) {
//pr_string( getcwd((char *)buffer, 4095));
pr_string("\n");
first = 0;
}
if((ifp = fopen(infilename, "r")) == NULL)
{
sprintf(Print_buffer, CANTLOAD, infilename);
errmsg(Print_buffer);
return(0);
}
else
{
save_cif = Curr_infile;
}
strcpy(Filename, infilename);
Curr_infile = ifp;
save_cof = Curr_outfile;
Curr_outfile = stdout;
while((the_list = read_list(status)) != NULL)
{
the_head = NODEPTR_HEAD(the_list);
if(NODEPTR_TYPE(the_head) == ATOM)
{
clauseptr = make_clause(the_list, NilNodeptr, status,
&the_pred);
}
else
clauseptr = make_clause(the_head, NODEPTR_TAIL(the_list), status, &the_pred);
if(!clauseptr)
{
fclose(Curr_infile);
return(0);
}
if(Trace_flag > 0){
pr_string("adding \n");
pr_clause(clauseptr);
pr_string("\n");
}
add_to_end(clauseptr, the_pred);
}
fclose(Curr_infile);
Curr_infile = save_cif;
Curr_outfile = save_cof;
return(1);
}
/******************************************************************************
load()
The usual function used to load a file.
Called in prmain.c
******************************************************************************/
int load(char *s)
{
ENTER("load");
return(do_consult(s, PERMANENT));
}
/*********************************************************************
add_to_end()
Adds a clause to the end of its packet.
********************************************************************/
void add_to_end(clause_ptr_t clauseptr,atom_ptr_t pred)
{
clause_ptr_t clp, previous;
ENTER("add_to_end");
clp = ATOMPTR_CLAUSE(pred);
record_pred(pred);
if(clp == NULL)
{
ATOMPTR_CLAUSE(pred) = clauseptr;
}
else
{
previous = clp;
while(clp != NULL)
{
previous = clp;
clp = CLAUSEPTR_NEXT(clp);
}
CLAUSEPTR_NEXT(previous) = clauseptr;
}
}
/**********************************************************************
record_pred()
Record the atom pointer as a predicate and verify we are not
redefining a primitive.
**********************************************************************/
void record_pred(atom_ptr_t atomptr)
{
extern unsigned int Inp_linecount;
pred_rec_ptr_t predptr, get_pred();
ENTER("record_pred");
if(atomptr < LastBuiltin)
{
sprintf(Print_buffer, "%s line %d ", Filename, Inp_linecount);
errmsg(Print_buffer);
fatal2("redefining builtin", ATOMPTR_NAME(atomptr));
}
if((atomptr < LastBuiltin) ||
(atomptr > LastBuiltin && ATOMPTR_CLAUSE(atomptr) == NULL))
{
predptr = get_pred();
predptr->atom = atomptr;
if( Pred_count == 0)
{
First_pred = predptr;
}
else
{
Last_pred->next_pred = predptr;
}
Last_pred = predptr;
Pred_count++;
}
}
/**********************************************************************
do_listing()
List all clauses on current output file.
**********************************************************************/
void do_listing()
{
pred_rec_ptr_t predptr;
ENTER("do_listing");
for(predptr = First_pred; predptr != NULL; predptr = predptr->next_pred)
{
if(predptr->atom > LastBuiltin)
{
pr_packet(ATOMPTR_CLAUSE(predptr->atom));
pr_string("\n");
}
}
}
/* end of file */