10 internal class Derivation
13 #region Private Static Variables
15 private static Dictionary<TokenType, Dictionary<string, Type>> derivates;
19 #region Private Methods
21 private string Derivate(Type destination, Type source)
23 return destination +
"-" + source;
26 private string Translate(Type type)
31 if (type == typeof(NullReference))
33 else if (type == typeof(
int))
35 else if (type == typeof(
float))
37 else if (type == typeof(
double))
39 else if (type == typeof(
bool))
41 else if (type == typeof(
string))
45 else if (type == typeof(
char))
48 throw new ParserException(
"Type '" + type.Name +
"' not supported by type inference system.");
53 #region Public Methods
58 if (derivates !=
null)
61 derivates =
new Dictionary<TokenType, Dictionary<String, Type>>();
63 Type typeNull = typeof(NullReference);
64 Type typeInt = typeof(
int);
65 Type typeFloat = typeof(
float);
66 Type typeDouble = typeof(
double);
67 Type typeBoolean = typeof(
bool);
68 Type typeString = typeof(
string);
70 Type typeChar = typeof(
char);
72 Dictionary<string, Type> Logic =
new Dictionary<string, Type>();
74 Logic[Derivate(
null,
null)] = typeBoolean;
75 Logic[Derivate(typeNull, typeNull)] = typeBoolean;
76 Logic[Derivate(
null, typeBoolean)] = typeBoolean;
77 Logic[Derivate(typeBoolean,
null)] = typeBoolean;
78 Logic[Derivate(typeBoolean, typeBoolean)] = typeBoolean;
83 Dictionary<string, Type> Comparision =
new Dictionary<string, Type>();
85 Comparision[Derivate(
null,
null)] = typeBoolean;
86 Comparision[Derivate(
null, typeNull)] = typeBoolean;
87 Comparision[Derivate(
null, typeInt)] = typeBoolean;
88 Comparision[Derivate(
null, typeFloat)] = typeBoolean;
89 Comparision[Derivate(
null, typeDouble)] = typeBoolean;
90 Comparision[Derivate(
null, typeBoolean)] = typeBoolean;
91 Comparision[Derivate(
null, typeString)] = typeBoolean;
92 Comparision[Derivate(
null, typeChar)] = typeBoolean;
94 Comparision[Derivate(typeNull,
null)] = typeBoolean;
95 Comparision[Derivate(typeNull, typeNull)] = typeBoolean;
97 Comparision[Derivate(typeInt,
null)] = typeBoolean;
98 Comparision[Derivate(typeInt, typeInt)] = typeBoolean;
99 Comparision[Derivate(typeInt, typeFloat)] = typeBoolean;
100 Comparision[Derivate(typeInt, typeDouble)] = typeBoolean;
102 Comparision[Derivate(typeFloat,
null)] = typeBoolean;
103 Comparision[Derivate(typeFloat, typeInt)] = typeBoolean;
104 Comparision[Derivate(typeFloat, typeFloat)] = typeBoolean;
105 Comparision[Derivate(typeFloat, typeDouble)] = typeBoolean;
107 Comparision[Derivate(typeChar,
null)] = typeBoolean;
108 Comparision[Derivate(typeChar, typeInt)] = typeBoolean;
109 Comparision[Derivate(typeChar, typeFloat)] = typeBoolean;
110 Comparision[Derivate(typeChar, typeString)] = typeBoolean;
111 Comparision[Derivate(typeChar, typeDouble)] = typeBoolean;
113 Comparision[Derivate(typeBoolean,
null)] = typeBoolean;
114 Comparision[Derivate(typeBoolean, typeBoolean)] = typeBoolean;
116 Comparision[Derivate(typeString,
null)] = typeBoolean;
117 Comparision[Derivate(typeString, typeString)] = typeBoolean;
118 Comparision[Derivate(typeString, typeChar)] = typeBoolean;
120 derivates[
TokenType.Equal] = Comparision;
121 derivates[
TokenType.NotEqual] = Comparision;
123 Dictionary<string, Type> Relation =
new Dictionary<string, Type>();
125 Relation[Derivate(
null,
null)] = typeBoolean;
126 Relation[Derivate(typeBoolean, typeBoolean)] = typeBoolean;
127 Relation[Derivate(
null, typeInt)] = typeBoolean;
128 Relation[Derivate(
null, typeFloat)] = typeBoolean;
129 Relation[Derivate(
null, typeDouble)] = typeBoolean;
130 Relation[Derivate(
null, typeString)] = typeBoolean;
131 Relation[Derivate(
null, typeChar)] = typeBoolean;
133 Relation[Derivate(typeInt,
null)] = typeBoolean;
134 Relation[Derivate(typeInt, typeInt)] = typeBoolean;
135 Relation[Derivate(typeInt, typeFloat)] = typeBoolean;
136 Relation[Derivate(typeInt, typeDouble)] = typeBoolean;
137 Relation[Derivate(typeInt, typeChar)] = typeBoolean;
139 Relation[Derivate(typeFloat,
null)] = typeBoolean;
140 Relation[Derivate(typeFloat, typeInt)] = typeBoolean;
141 Relation[Derivate(typeFloat, typeFloat)] = typeBoolean;
142 Relation[Derivate(typeFloat, typeChar)] = typeBoolean;
143 Relation[Derivate(typeFloat, typeDouble)] = typeBoolean;
145 Relation[Derivate(typeChar,
null)] = typeBoolean;
146 Relation[Derivate(typeChar, typeInt)] = typeBoolean;
147 Relation[Derivate(typeChar, typeFloat)] = typeBoolean;
148 Relation[Derivate(typeChar, typeChar)] = typeBoolean;
149 Relation[Derivate(typeChar, typeString)] = typeBoolean;
150 Relation[Derivate(typeChar, typeDouble)] = typeBoolean;
152 Relation[Derivate(typeString,
null)] = typeBoolean;
153 Relation[Derivate(typeString, typeString)] = typeBoolean;
154 Relation[Derivate(typeString, typeChar)] = typeBoolean;
157 derivates[
TokenType.GreaterEqual] = Relation;
159 derivates[
TokenType.LessEqual] = Relation;
161 Dictionary<string, Type>
Plus =
new Dictionary<string, Type>();
163 Plus[Derivate(
null,
null)] =
null;
164 Plus[Derivate(
null, typeInt)] =
null;
165 Plus[Derivate(
null, typeFloat)] =
null;
166 Plus[Derivate(
null, typeDouble)] =
null;
167 Plus[Derivate(
null, typeBoolean)] =
null;
168 Plus[Derivate(
null, typeString)] =
null;
169 Plus[Derivate(
null, typeChar)] =
null;
170 Plus[Derivate(
null, typeArray)] = typeArray;
172 Plus[Derivate(typeInt,
null)] =
null;
173 Plus[Derivate(typeInt, typeInt)] = typeInt;
174 Plus[Derivate(typeInt, typeFloat)] = typeFloat;
175 Plus[Derivate(typeInt, typeChar)] = typeChar;
176 Plus[Derivate(typeInt, typeDouble)] = typeDouble;
178 Plus[Derivate(typeFloat,
null)] =
null;
179 Plus[Derivate(typeFloat, typeInt)] = typeFloat;
180 Plus[Derivate(typeFloat, typeFloat)] = typeFloat;
181 Plus[Derivate(typeFloat, typeChar)] = typeFloat;
182 Plus[Derivate(typeFloat, typeDouble)] = typeDouble;
184 Plus[Derivate(typeString,
null)] = typeString;
185 Plus[Derivate(typeString, typeInt)] = typeString;
186 Plus[Derivate(typeString, typeFloat)] = typeString;
187 Plus[Derivate(typeString, typeBoolean)] = typeString;
188 Plus[Derivate(typeString, typeString)] = typeString;
189 Plus[Derivate(typeString, typeChar)] = typeString;
190 Plus[Derivate(typeString, typeArray)] = typeArray;
191 Plus[Derivate(typeString, typeDouble)] = typeString;
193 Plus[Derivate(typeArray,
null)] = typeArray;
194 Plus[Derivate(typeArray, typeInt)] = typeArray;
195 Plus[Derivate(typeArray, typeFloat)] = typeArray;
196 Plus[Derivate(typeArray, typeBoolean)] = typeArray;
197 Plus[Derivate(typeArray, typeString)] = typeArray;
198 Plus[Derivate(typeArray, typeChar)] = typeArray;
199 Plus[Derivate(typeArray, typeArray)] = typeArray;
200 Plus[Derivate(typeArray, typeDouble)] = typeArray;
202 Plus[Derivate(typeChar,
null)] = typeChar;
203 Plus[Derivate(typeChar, typeInt)] = typeChar;
204 Plus[Derivate(typeChar, typeFloat)] = typeChar;
205 Plus[Derivate(typeChar, typeChar)] = typeChar;
206 Plus[Derivate(typeChar, typeDouble)] = typeDouble;
210 Dictionary<string, Type>
Minus =
new Dictionary<string, Type>();
212 Minus[Derivate(
null,
null)] =
null;
213 Minus[Derivate(
null, typeInt)] =
null;
214 Minus[Derivate(
null, typeFloat)] =
null;
215 Minus[Derivate(
null, typeBoolean)] =
null;
216 Minus[Derivate(
null, typeString)] =
null;
217 Minus[Derivate(
null, typeChar)] =
null;
218 Minus[Derivate(
null, typeArray)] =
null;
220 Minus[Derivate(typeInt,
null)] =
null;
221 Minus[Derivate(typeInt, typeInt)] = typeInt;
222 Minus[Derivate(typeInt, typeFloat)] = typeFloat;
223 Minus[Derivate(typeInt, typeChar)] = typeChar;
225 Minus[Derivate(typeFloat,
null)] =
null;
226 Minus[Derivate(typeFloat, typeInt)] = typeFloat;
227 Minus[Derivate(typeFloat, typeFloat)] = typeFloat;
228 Minus[Derivate(typeFloat, typeChar)] = typeFloat;
230 Minus[Derivate(typeString,
null)] = typeString;
231 Minus[Derivate(typeString, typeString)] = typeString;
232 Minus[Derivate(typeString, typeChar)] = typeString;
234 Minus[Derivate(typeArray,
null)] = typeArray;
235 Minus[Derivate(typeArray, typeInt)] = typeArray;
236 Minus[Derivate(typeArray, typeFloat)] = typeArray;
237 Minus[Derivate(typeArray, typeBoolean)] = typeArray;
238 Minus[Derivate(typeArray, typeString)] = typeArray;
239 Minus[Derivate(typeArray, typeChar)] = typeArray;
240 Minus[Derivate(typeArray, typeArray)] = typeArray;
242 Minus[Derivate(typeChar,
null)] = typeChar;
243 Minus[Derivate(typeChar, typeInt)] = typeChar;
244 Minus[Derivate(typeChar, typeFloat)] = typeChar;
245 Minus[Derivate(typeChar, typeChar)] = typeChar;
249 Dictionary<string, Type> Factor =
new Dictionary<string, Type>();
251 Factor[Derivate(
null,
null)] =
null;
252 Factor[Derivate(
null, typeInt)] =
null;
253 Factor[Derivate(
null, typeFloat)] = typeFloat;
254 Factor[Derivate(
null, typeChar)] = typeChar;
256 Factor[Derivate(typeInt,
null)] =
null;
257 Factor[Derivate(typeInt, typeInt)] = typeInt;
258 Factor[Derivate(typeInt, typeFloat)] = typeFloat;
259 Factor[Derivate(typeInt, typeChar)] = typeChar;
261 Factor[Derivate(typeFloat,
null)] = typeFloat;
262 Factor[Derivate(typeFloat, typeInt)] = typeFloat;
263 Factor[Derivate(typeFloat, typeFloat)] = typeFloat;
264 Factor[Derivate(typeFloat, typeChar)] = typeFloat;
266 Factor[Derivate(typeChar,
null)] = typeFloat;
267 Factor[Derivate(typeChar, typeInt)] = typeFloat;
268 Factor[Derivate(typeChar, typeFloat)] = typeFloat;
269 Factor[Derivate(typeChar, typeChar)] = typeChar;
284 public Type Derivate(
Token token, Type destination, Type source)
287 if (!derivates.ContainsKey(token.
Type))
288 throw new ParserException(
"Token '" + token +
"' ist kein binary operator.");
290 Dictionary<string, Type> derivate = derivates[token.
Type];
292 string key = Derivate(destination, source);
294 if (!derivate.ContainsKey(key))
297 string dst = Translate(destination);
298 string src = Translate(source);
300 throw new ParserException(
"Die Operation '" + token.
Lexeme +
"' kann '" + Translate(destination) +
"' und '" + Translate(source) +
"' nicht ableiten.");
304 return derivate[key];