comparison anagram/agcore/rule.cpp @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children 607e3be6bad8
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 /*
2 * AnaGram, A System for Syntax Directed Programming
3 * Copyright 1993-2002 Parsifal Software. All Rights Reserved.
4 * See the file COPYING for license and usage terms.
5 *
6 * rule.cpp
7 */
8
9 #include "agdict.h"
10 #include "config.h"
11 #include "data.h"
12 #include "error.h"
13 #include "q1glbl.h"
14 #include "rproc.h"
15 #include "rule.h"
16 #include "stacks.h"
17
18 //#define INCLUDE_LOGGING
19 #include "log.h"
20
21
22 Rule::Map map_form_number;
23 const int Rule::first = 1;
24
25 RuleDescriptor::RuleDescriptor()
26 : line(0) , col(0)
27 , proc_name(0)
28 , non_vanishing_length(0)
29 , op_bias(0)
30 , precedence_level(0)
31 , not_unique_reduction(0)
32 , error_mask(0)
33 , left_associative(0)
34 , right_associative(0)
35 , fast_loop(0)
36 , immediate_proc(0)
37 , lexical(0)
38 , nonLexical(0)
39 , reductionRequired(0)
40 {
41 // Nothing to do
42 }
43
44 Rule::Rule() : ObjectRegister<RuleDescriptor>() {}
45 Rule::Rule(int x) : ObjectRegister<RuleDescriptor>(x) {}
46 Rule::Rule(const Rule &t) : ObjectRegister<RuleDescriptor>(t) {}
47
48
49 RuleDescriptor &Rule::Map::operator [] (unsigned x) {
50 //LOGSECTION("RuleDescriptor::operator[]");
51 //LOGV(x) LCV(Rule::descriptorList.size());
52 assert(x < Rule::descriptorList.size());
53 return Rule::descriptorList[x];
54 }
55
56 Rule Rule::create() {
57 LOGSECTION("Rule::create");
58 nforms = descriptorList.size();
59 descriptorList.push(RuleDescriptor());
60 Rule rule(nforms);
61 LOGV(nforms) LCV(descriptorList.size());
62 return rule;
63 }
64
65 void Rule::reset() {
66 LOGSECTION("Rule::reset");
67 descriptorList.reset();
68 nforms = descriptorList.size();
69 LOGV(nforms) LCV(descriptorList.size());
70 }
71
72 Token &RuleDescriptor::token(int x) {
73 assert((unsigned) x < elementList.size());
74 return elementList[x].token;
75 }
76
77 Token &Rule::token(int x) const {
78 return descriptor->token(x);
79 }
80
81 AgStringDictionary cVariableList;
82 const int Cast::first = 1;
83 static AgString compareObject;
84
85 int treeCompare(AgStack<AgString> &descriptorList, const int &x, const int &y){
86 LOGSECTION("treeCompare");
87 const AgString &dx =
88 (unsigned) x < descriptorList.size() ? descriptorList[x] : compareObject;
89 const AgString &dy =
90 (unsigned) y < descriptorList.size() ? descriptorList[y] : compareObject;
91 int flag = dx < dy ? -1 : dy < dx;
92 LOGV(x) LCV(y) LCV(flag);
93 return flag;
94 }
95
96 CastDescriptor::CastDescriptor() : wrapper(0) {}
97 CastDescriptor::CastDescriptor(const char *s) : name(s), wrapper(0) {}
98
99 Cast::Cast() : KeyedObjectRegister<CastDescriptor>() {}
100 Cast::Cast(const int x) : KeyedObjectRegister<CastDescriptor>(x) {}
101
102 Cast::Cast(const char *s)
103 : KeyedObjectRegister<CastDescriptor>(s)
104 {
105 LOGSECTION("Cast::Cast(const char *)");
106 LOGV(index) LCV(s);
107 #if 0 /* NOTUSED */
108 compareObject = s;
109 for (int i = 0; i < count(); i++) {
110 LOGV(i) LCV(descriptorList[i]) LCV(descriptorList[i].size());
111 int indexI = treeCompare(descriptorList, index, i);
112 int iIndex = treeCompare(descriptorList, i, index);
113 LOGV(indexI) LCV(iIndex);
114 }
115 #endif
116 // Nothing to do
117 }
118
119 Cast::Cast(const AgString s)
120 : KeyedObjectRegister<CastDescriptor>(s.pointer())
121 {
122 #if 0 /* NOTUSED */
123 LOGSECTION("Cast::Cast(const AgString)");
124 LOGV(index) LCV(s);
125 compareObject = s;
126 for (int i = 0; i < count(); i++) {
127 LOGV(i) LCV(descriptorList[i]) LCV(descriptorList[i].size());
128 int indexI = treeCompare(descriptorList, index, i);
129 int iIndex = treeCompare(descriptorList, i, index);
130 LOGV(indexI) LCV(iIndex);
131 }
132 #endif
133 // Nothing to do
134 }
135
136 void Cast::reset() {
137 KeyedObjectRegister<CastDescriptor>::reset();
138 Cast nullCast = "";
139 voidCast = Cast("void");
140 intCast = Cast("int");
141 longCast = Cast("long");
142 nWrappers = 0;
143 #if 0 /* NOTUSED */
144 for (unsigned i = 0; i < count(); i++) {
145 LOGV(i) LCV(descriptorList[i]) LCV(descriptorList[i].size());
146 }
147 #endif
148 default_token_type = void_token_type = voidCast;
149 default_input_type = int_token_type = intCast;
150 parser_stack_alignment = long_token_type = longCast;
151 LOGV((int) voidCast) LCV((AgString)voidCast);
152 LOGV((int) intCast) LCV((AgString)intCast);
153 LOGV(default_token_type);
154 LOGV(default_input_type);
155 LOGV(int_token_type);
156 }
157
158 void Cast::requireWrapper() {
159 if (index == default_token_type) {
160 log_error("Cannot make wrapper for default token type");
161 return;
162 }
163 nWrappers++;
164 descriptor->wrapper = 1;
165 }
166
167 int Cast::wrapperRequired() {
168 return descriptor->wrapper != 0;
169 }
170
171 Cast Cast::voidCast;
172 Cast Cast::intCast;
173 Cast Cast::longCast;
174 int Cast::nWrappers = 0;
175
176 Cast Cast::create() {
177 LOGSECTION("Cast::create");
178 #if 0
179 for (int i = 0; i < count(); i++) {
180 LOGV(i) LCV(descriptorList[i]) LCV(descriptorList[i].size());
181 }
182 #endif
183 tss();
184 Cast type = string_base;
185 //char *string = string_base;
186 LOGV(string_base) LCV(tis()) LCV((int) type) LCV((AgString)type);
187 rcs();
188 #if 0
189 for (int i = 0; i < count(); i++) {
190 LOGV(i) LCV(descriptorList[i]) LCV(descriptorList[i].size());
191 }
192 #endif
193
194 return type;
195 }
196
197 int Cast::operator <(const Cast x) const {
198 return *descriptor < *x.descriptor;
199 }
200
201 RuleElement::RuleElement(const Token &t, int x) : token(t), cVariable(x) {}
202
203 int RuleElement::operator < (const RuleElement x) const {
204 if (token < x.token) return 1;
205 if (x.token < token) return 0;
206 return cVariable < x.cVariable;
207 }
208
209 VpRuleDescriptor::VpRuleDescriptor() : procedureName(0) {}
210 VpRuleDescriptor::VpRuleDescriptor(AgStack<RuleElement> &stack, int pn)
211 : elementList(stack), procedureName(pn)
212 {
213 // Nothing to do
214 }
215
216 int VpRuleDescriptor::operator < (const VpRuleDescriptor x) const {
217 if (elementList < x.elementList) return 1;
218 if (x.elementList < elementList) return 0;
219 return procedureName < procedureName;
220 }
221
222 VpRule::VpRule() : KeyedObjectRegister<VpRuleDescriptor>() {}
223 VpRule::VpRule(const int x) : KeyedObjectRegister<VpRuleDescriptor>(x) {}
224 VpRule::VpRule(const VpRule &x) : KeyedObjectRegister<VpRuleDescriptor>(x) {}
225 VpRule::VpRule(AgStack<RuleElement> &stack, int pn)
226 : KeyedObjectRegister<VpRuleDescriptor>(VpRuleDescriptor(stack, pn))
227 {
228 // Nothing to do
229 }
230
231 void VpRule::reset() {
232 KeyedObjectRegister<VpRuleDescriptor>::reset();
233 AgStack<RuleElement> emptyStack;
234 VpRule rule(emptyStack, 0);
235 }
236
237 Procedure::Procedure(const int x) : ObjectRegister<ProcedureDescriptor>(x) {}
238
239 Procedure::Procedure(const CSegment &t)
240 : ObjectRegister<ProcedureDescriptor>(ProcedureDescriptor(t))
241 {
242 LOGSECTION("Procedure::Procedure");
243 LOGV(index);
244 // Nothing to do
245 }
246
247 ProcedureDescriptor::ProcedureDescriptor()
248 : cast(0)
249 , alias(0)
250 , line(0)
251 , col(0)
252 , form_number(0)
253 , macro_flag(0)
254 , value_flag(0)
255 {
256 // Nothing to do
257 }
258
259 ProcedureDescriptor::ProcedureDescriptor(const CSegment &s)
260 : cast(0)
261 , alias(0)
262 , line(0)
263 , col(0)
264 , form_number(0)
265 , cSegment(s)
266 , macro_flag(0)
267 , value_flag(0)
268 {
269 // Nothing to do
270 }
271
272 const int Procedure::first = 1;
273
274 void Procedure::reset() {
275 LOGSECTION("Procedure::reset");
276 descriptorList.reset();
277 CSegment segment;
278 segment.line = 0;
279 segment.begin = segment.end = 0;
280 Procedure nullProc(segment);
281 LOGV(nullProc) LCV(descriptorList.size());
282 }
283
284 int Procedure::operator < (const Procedure p) const {
285 return *descriptor < *p.descriptor;
286 }
287