Debian Codegraph @

terps/alan3/AltInfo.c

raw symbols SCIP
1 #include "AltInfo.h"
2
3 /*----------------------------------------------------------------------*\
4
5 An info node about the Verb Alternatives found and possibly executed
6
7 \*----------------------------------------------------------------------*/
8
9 #include "types.h"
10
11 #include "checkentry.h"
12 #include "debug.h"
13 #include "inter.h"
14 #include "lists.h"
15 #include "instance.h"
16 #include "options.h"
17 #include "memory.h"
18 #include "current.h"
19 #include "class.h"
20 #include "params.h"
21 #include "literal.h"
22 #include "syntax.h"
23
24
25 /* Types */
26 typedef AltInfo *AltInfoFinder(int verb, Parameter parameters[]);
27
28
29 /*======================================================================*/
30 void primeAltInfo(AltInfo *altInfo, int level, int parameter, int instance, int class)
31 {
32 altInfo->level = level;
33 altInfo->parameter = parameter;
34 altInfo->instance = instance;
35 altInfo->class = class;
36 altInfo->done = false;
37 altInfo->end = false;
38 }
39
40
41 /*----------------------------------------------------------------------*/
42 static void traceInstanceAndItsClass(Aid instance, Aid class)
43 {
44 traceSay(instance);
45 printf("[%d]", instance);
46 if (class != NO_CLASS)
47 printf(", inherited from %s[%d]", idOfClass(class), class);
48 }
49
50
51 /*----------------------------------------------------------------------*/
52 static void traceAltInfo(AltInfo *alt) {
53 switch (alt->level) {
54 case GLOBAL_LEVEL:
55 printf("GLOBAL");
56 break;
57 case LOCATION_LEVEL:
58 printf("in (location) ");
59 traceInstanceAndItsClass(current.location, alt->class);
60 break;
61 case PARAMETER_LEVEL: {
62 char *parameterName = parameterNameInSyntax(current.verb, alt->parameter);
63 if (parameterName != NULL)
64 printf("in parameter '%s'(#%d)=", parameterName, alt->parameter);
65 else
66 printf("in parameter #%d=", alt->parameter);
67 traceInstanceAndItsClass(globalParameters[alt->parameter-1].instance, alt->class);
68 break;
69 }
70 }
71 }
72
73
74 /*----------------------------------------------------------------------*/
75 static void traceVerbCheck(AltInfo *alt, bool execute)
76 {
77 if (traceSectionOption && execute) {
78 printf("\n<VERB %d, ", current.verb);
79 traceAltInfo(alt);
80 printf(", CHECK:>\n");
81 }
82 }
83
84
85 /*======================================================================*/
86 bool checkFailed(AltInfo *altInfo, bool execute)
87 {
88 if (altInfo->alt != NULL && altInfo->alt->checks != 0) {
89 traceVerbCheck(altInfo, execute);
90 // TODO Why does this not generate a regression error with !
91 // Need a new regression case?
92 fail = false;
93 if (checksFailed(altInfo->alt->checks, execute)) return true;
94 if (fail) return true;
95 }
96 return false;
97 }
98
99
100 /*----------------------------------------------------------------------*/
101 static void traceVerbExecution(AltInfo *alt)
102 {
103 if (traceSectionOption) {
104 printf("\n<VERB %d, ", current.verb);
105 traceAltInfo(alt);
106 printf(", DOES");
107 switch (alt->alt->qual) {
108 case Q_BEFORE: printf(" (BEFORE)");
109 break;
110 case Q_ONLY: printf(" (ONLY)");
111 break;
112 case Q_AFTER: printf(" (AFTER)");
113 break;
114 case Q_DEFAULT:
115 break;
116 }
117 printf(":>\n");
118 }
119 }
120
121
122 /*======================================================================*/
123 bool executedOk(AltInfo *altInfo)
124 {
125 fail = false;
126 if (!altInfo->done && altInfo->alt->action != 0) {
127 traceVerbExecution(altInfo);
128 current.instance = altInfo->instance;
129 interpret(altInfo->alt->action);
130 }
131 altInfo->done = true;
132 return !fail;
133 }
134
135
136 /*======================================================================*/
137 bool canBeExecuted(AltInfo *altInfo) {
138 return altInfo->alt != NULL && altInfo->alt->action != 0;
139 }
140
141
142 /*======================================================================*/
143 AltInfo *duplicateAltInfoArray(AltInfo original[]) {
144 int size;
145 AltInfo *duplicate;
146
147 for (size = 0; original[size].end != true; size++)
148 ;
149 size++;
150 duplicate = allocate(size*sizeof(AltInfo));
151 memcpy(duplicate, original, size*sizeof(AltInfo));
152 return duplicate;
153 }
154
155
156 /*======================================================================*/
157 int lastAltInfoIndex(AltInfo altInfo[])
158 {
159 int altIndex;
160
161 /* Loop to last alternative */
162 for (altIndex = -1; !altInfo[altIndex+1].end; altIndex++)
163 ;
164 return altIndex;
165 }
166
167
168 /*----------------------------------------------------------------------*/
169 static AltInfo *nextFreeAltInfo(AltInfoArray altInfos) {
170 return &altInfos[lastAltInfoIndex(altInfos)+1];
171 }
172
173
174 /*----------------------------------------------------------------------*/
175 static void addAlternative(AltInfoArray altInfos, int verb, int level, Aint parameterNumber, Aint theClass, Aid theInstance, AltEntryFinder finder) {
176 AltInfo *altInfoP = nextFreeAltInfo(altInfos);
177
178 altInfoP->alt = (*finder)(verb, parameterNumber, theInstance, theClass);
179 if (altInfoP->alt != NULL) {
180 primeAltInfo(altInfoP, level, parameterNumber, theInstance, theClass);
181 altInfoP[1].end = true;
182 }
183 }
184
185
186 /*----------------------------------------------------------------------*/
187 static void addGlobalAlternatives(AltInfoArray altInfos, int verb, AltEntryFinder finder ) {
188 addAlternative(altInfos, verb, GLOBAL_LEVEL, NO_PARAMETER, NO_CLASS, NO_INSTANCE, finder);
189 }
190
191
192 /*----------------------------------------------------------------------*/
193 static void addAlternativesFromParents(AltInfoArray altInfos, int verb, int level, Aint parameterNumber, Aint theClass, Aid theInstance, AltEntryFinder finder){
194 if (classes[theClass].parent != 0)
195 addAlternativesFromParents(altInfos, verb, level,
196 parameterNumber,
197 classes[theClass].parent,
198 theInstance,
199 finder);
200
201 addAlternative(altInfos, verb, level, parameterNumber, theClass, theInstance, finder);
202 }
203
204
205 /*----------------------------------------------------------------------*/
206 static void addAlternativesFromLocation(AltInfoArray altInfos, int verb, Aid location, AltEntryFinder finder) {
207 if (admin[location].location != 0)
208 addAlternativesFromLocation(altInfos, verb, admin[location].location, finder);
209
210 addAlternativesFromParents(altInfos, verb,
211 LOCATION_LEVEL,
212 NO_PARAMETER,
213 instances[location].parent,
214 location,
215 finder);
216
217 addAlternative(altInfos, verb, LOCATION_LEVEL, NO_PARAMETER, NO_CLASS, location, finder);
218 }
219
220
221 /*----------------------------------------------------------------------*/
222 static void addAlternativesFromParameter(AltInfoArray altInfos, int verb, Parameter parameters[], int parameterNumber, AltEntryFinder finder) {
223 Aid parent;
224 Aid theInstance = parameters[parameterNumber-1].instance;
225
226 if (isLiteral(theInstance))
227 parent = literals[literalFromInstance(theInstance)].class;
228 else
229 parent = instances[theInstance].parent;
230 addAlternativesFromParents(altInfos, verb, PARAMETER_LEVEL, parameterNumber, parent, theInstance, finder);
231
232 if (!isLiteral(theInstance))
233 addAlternative(altInfos, verb, PARAMETER_LEVEL, parameterNumber, NO_CLASS, theInstance, finder);
234 }
235
236
237 /*======================================================================*/
238 bool anyCheckFailed(AltInfoArray altInfo, bool execute)
239 {
240 int altIndex;
241
242 if (altInfo != NULL)
243 for (altIndex = 0; !altInfo[altIndex].end; altIndex++) {
244 current.instance = altInfo[altIndex].instance;
245 if (checkFailed(&altInfo[altIndex], execute))
246 return true;
247 }
248 return false;
249 }
250
251
252 /*======================================================================*/
253 bool anythingToExecute(AltInfo altInfo[])
254 {
255 int altIndex;
256
257 /* Check for anything to execute... */
258 if (altInfo != NULL)
259 for (altIndex = 0; !altInfo[altIndex].end; altIndex++)
260 if (canBeExecuted(&altInfo[altIndex]))
261 return true;
262 return false;
263 }
264
265
266 /*----------------------------------------------------------------------*/
267 static VerbEntry *findVerbEntry(int verbCode, VerbEntry *entries) {
268 VerbEntry *verbEntry;
269 for (verbEntry = entries; !isEndOfArray(verbEntry); verbEntry++) {
270 if (verbEntry->code < 0) {
271 /* Verb codes are negative for Meta verbs, if so they are also 1 off to avoid EOF */
272 if (abs(verbEntry->code)-1 == verbCode)
273 return verbEntry;
274 } else {
275 if (verbEntry->code == verbCode)
276 return verbEntry;
277 }
278 }
279 return NULL;
280 }
281
282
283 /*----------------------------------------------------------------------*/
284 static AltEntry *findAlternative(Aaddr verbTableAddress, int verbCode, int parameterNumber)
285 {
286 AltEntry *alt;
287 VerbEntry *verbEntry;
288
289 if (verbTableAddress == 0) return NULL;
290
291 verbEntry = findVerbEntry(verbCode, (VerbEntry *) pointerTo(verbTableAddress));
292 if (verbEntry != NULL)
293 for (alt = (AltEntry *) pointerTo(verbEntry->alts); !isEndOfArray(alt); alt++) {
294 if (alt->param == parameterNumber || alt->param == 0) {
295 if (verbEntry->code < 0) current.meta = true;
296 return alt;
297 }
298 }
299 return NULL;
300 }
301
302
303 /*----------------------------------------------------------------------*/
304 static AltEntry *alternativeFinder(int verb, int parameterNumber, int theInstance, int theClass)
305 {
306 if (theClass != NO_CLASS)
307 return findAlternative(classes[theClass].verbs, verb, parameterNumber);
308 else if (theInstance != NO_INSTANCE)
309 return findAlternative(instances[theInstance].verbs, verb, parameterNumber);
310 else
311 return findAlternative(header->verbTableAddress, verb, parameterNumber);
312 }
313
314
315 /*======================================================================*/
316 AltInfo *findAllAlternatives(int verb, Parameter parameters[]) {
317 int parameterNumber;
318 AltInfo altInfos[1000];
319 altInfos[0].end = true;
320
321 addGlobalAlternatives(altInfos, verb, &alternativeFinder);
322
323 addAlternativesFromLocation(altInfos, verb, current.location, &alternativeFinder);
324
325 for (parameterNumber = 1; !isEndOfArray(&parameters[parameterNumber-1]); parameterNumber++) {
326 addAlternativesFromParameter(altInfos, verb, parameters, parameterNumber, &alternativeFinder);
327 }
328 return duplicateAltInfoArray(altInfos);
329 }
330
331
332 /*----------------------------------------------------------------------*/
333 static bool possibleWithFinder(int verb, Parameter parameters[], AltInfoFinder *finder) {
334 bool anything;
335 AltInfo *allAlternatives;
336
337 allAlternatives = finder(verb, parameters);
338
339 // TODO Need to do this since anyCheckFailed() call execute() which assumes the global parameters
340 setGlobalParameters(parameters);
341 if (anyCheckFailed(allAlternatives, DONT_EXECUTE_CHECK_BODY_ON_FAIL))
342 anything = false;
343 else
344 anything = anythingToExecute(allAlternatives);
345
346 if (allAlternatives != NULL)
347 deallocate(allAlternatives);
348
349 return(anything);
350 }
351
352
353
354 /*======================================================================*/
355 bool possible(int verb, Parameter inParameters[], ParameterPosition parameterPositions[])
356 {
357 // This is a wrapper for possibleWithFinder() which is used in unit tests
358 // possible() should be used "for real".
359
360 return possibleWithFinder(verb, inParameters, findAllAlternatives);
361 }