Debian Codegraph @

terps/alan3/rules.c

raw symbols SCIP
1 /*----------------------------------------------------------------------*\
2
3 rules.c
4
5 Rule handling unit of Alan interpreter module, ARUN.
6
7 \*----------------------------------------------------------------------*/
8
9 #include "types.h"
10 #include "rules.h"
11
12
13 /* IMPORTS */
14 #include "lists.h"
15 #include "inter.h"
16 #include "debug.h"
17 #include "current.h"
18 #include "options.h"
19 #include "compatibility.h"
20
21 #ifdef HAVE_GLK
22 #include "glkio.h"
23 #endif
24
25
26 /* PUBLIC DATA: */
27 RuleEntry *rules; /* Rule table pointer */
28 bool anyRuleRun;
29
30
31 /* PRIVATE TYPES: */
32 typedef struct RulesAdmin {
33 bool lastEval;
34 bool alreadyRun;
35 } RulesAdmin;
36
37 /* PRIVATE DATA: */
38 static int ruleCount;
39 static RulesAdmin *rulesAdmin; /* Table for administration of the rules */
40
41 /*----------------------------------------------------------------------*/
42 static void clearRulesAdmin(int ruleCount) {
43 int r;
44 for (r = 0; r < ruleCount; r++) {
45 rulesAdmin[r].lastEval = false;
46 rulesAdmin[r].alreadyRun = false;
47 }
48 }
49
50
51 /*----------------------------------------------------------------------*/
52 static void initRulesAdmin(int ruleCount) {
53 int r;
54
55 rulesAdmin = allocate(ruleCount*sizeof(RulesAdmin)+sizeof(EOF));
56 for (r = 0; r < ruleCount; r++)
57 ;
58 setEndOfArray(&rulesAdmin[r]);
59 }
60
61
62 /*======================================================================*/
63 void initRules(Aaddr ruleTableAddress) {
64
65 rules = (RuleEntry *) pointerTo(ruleTableAddress);
66
67 if (ruleCount == 0) { /* Not initiated */
68 for (ruleCount = 0; !isEndOfArray(&rules[ruleCount]); ruleCount++)
69 ;
70 initRulesAdmin(ruleCount);
71 }
72 clearRulesAdmin(ruleCount);
73 }
74
75
76 /*----------------------------------------------------------------------*/
77 static void traceRuleStart(int rule, char *what) {
78 printf("\n<RULE %d", rule);
79 if (current.location != 0) {
80 printf(" (at ");
81 traceSay(current.location);
82 } else
83 printf(" (nowhere");
84 printf("[%d]), %s", current.location, what);
85 }
86
87 static bool detailedTraceOn() {
88 return traceInstructionOption || traceSourceOption || tracePushOption || traceStackOption;
89 }
90
91
92 /*----------------------------------------------------------------------*/
93 static void traceRuleEvaluation(int rule) {
94 if (traceSectionOption) {
95 if (detailedTraceOn()) {
96 traceRuleStart(rule, "Evaluating:>");
97 if (!traceInstructionOption)
98 printf("\n");
99 } else {
100 traceRuleStart(rule, "Evaluating to ");
101 }
102 }
103 }
104
105 /*----------------------------------------------------------------------*/
106 static void traceRuleResult(int rule, bool result) {
107 if (traceSectionOption) {
108 if (detailedTraceOn())
109 printf("\n<RULE %d %s%s", rule, "Evaluated to ", result?": true>\n":": false>\n");
110 else
111 printf(result?"true":"false");
112 }
113 }
114
115 /*----------------------------------------------------------------------*/
116 static void traceRuleExecution(int rule) {
117 if (traceSectionOption) {
118 if (!traceInstructionOption && !traceSourceOption)
119 printf(", Executing:>\n");
120 else {
121 traceRuleStart(rule, "Executing:>");
122 if (!traceInstructionOption)
123 printf("\n");
124 }
125 }
126 }
127
128
129
130 /*----------------------------------------------------------------------*/
131 static void evaluateRulesPreBeta2(void)
132 {
133 bool change = true;
134 int i;
135
136 for (i = 1; !isEndOfArray(&rules[i-1]); i++)
137 rules[i-1].alreadyRun = false;
138
139 while (change) {
140 change = false;
141 for (i = 1; !isEndOfArray(&rules[i-1]); i++)
142 if (!rules[i-1].alreadyRun) {
143 traceRuleEvaluation(i);
144 if (evaluate(rules[i-1].exp)) {
145 change = true;
146 rules[i-1].alreadyRun = true;
147 traceRuleExecution(i);
148 interpret(rules[i-1].stms);
149 } else if (traceSectionOption && !traceInstructionOption)
150 printf(":>\n");
151 }
152 }
153 }
154
155
156 /*----------------------------------------------------------------------*/
157 /* This is how beta2 thought rules should be evaluated:
158 */
159 static void evaluateRulesBeta2(void)
160 {
161 bool change = true;
162 int i;
163
164 for (i = 1; !isEndOfArray(&rules[i-1]); i++)
165 rules[i-1].alreadyRun = false;
166
167 current.location = NOWHERE;
168 current.actor = 0;
169
170 while (change) {
171 change = false;
172 for (i = 1; !isEndOfArray(&rules[i-1]); i++)
173 if (!rules[i-1].alreadyRun) {
174 traceRuleEvaluation(i);
175 bool triggered = evaluate(rules[i-1].exp);
176 if (triggered) {
177 if (rulesAdmin[i-1].lastEval == false) {
178 change = true;
179 rules[i-1].alreadyRun = true;
180 traceRuleExecution(i);
181 interpret(rules[i-1].stms);
182 }
183 rulesAdmin[i-1].lastEval = triggered;
184 } else {
185 rulesAdmin[i-1].lastEval = false;
186 if (traceSectionOption && !traceInstructionOption)
187 printf(":>\n");
188 }
189 }
190 }
191 }
192
193
194 /*======================================================================*/
195 void resetRules() {
196 int i;
197 for (i = 1; !isEndOfArray(&rules[i-1]); i++) {
198 rulesAdmin[i-1].alreadyRun = false;
199 }
200 }
201
202
203 /*======================================================================*/
204 void evaluateRules(RuleEntry rules[]) {
205 bool change = true;
206 int rule;
207
208 current.location = NOWHERE;
209 current.actor = 0;
210
211 while (change) {
212 change = false;
213 for (rule = 1; !isEndOfArray(&rules[rule-1]); rule++) {
214 traceRuleEvaluation(rule);
215 bool evaluated_value = evaluate(rules[rule-1].exp);
216 traceRuleResult(rule, evaluated_value);
217 if (evaluated_value == true && rulesAdmin[rule-1].lastEval == false
218 && !rulesAdmin[rule-1].alreadyRun) {
219 change = true;
220 traceRuleExecution(rule);
221 interpret(rules[rule-1].stms);
222 rulesAdmin[rule-1].alreadyRun = true;
223 anyRuleRun = true;
224 } else {
225 if (traceSectionOption && !(traceInstructionOption || traceSourceOption)) {
226 if (rulesAdmin[rule-1].alreadyRun)
227 printf(": Already run now");
228 if (rulesAdmin[rule-1].lastEval == true)
229 printf(": Already true at last run");
230 printf(":>\n");
231 }
232 }
233 rulesAdmin[rule-1].lastEval = evaluated_value;
234 }
235 }
236 }
237
238
239 /*=======================================================================*/
240 void resetAndEvaluateRules(RuleEntry rules[], char *version, Stack theStack) {
241 if (isPreBeta2(version))
242 evaluateRulesPreBeta2();
243 else if (isPreBeta3(version))
244 evaluateRulesBeta2();
245 else {
246 resetRules();
247 evaluateRules(rules);
248 }
249 }