ScriptStack 1.0.4
Loading...
Searching...
No Matches
Derivation.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Text;
4
6
8{
9
10 internal class Derivation
11 {
12
13 #region Private Static Variables
14
15 private static Dictionary<TokenType, Dictionary<string, Type>> derivates;
16
17 #endregion
18
19 #region Private Methods
20
21 private string Derivate(Type destination, Type source)
22 {
23 return destination + "-" + source;
24 }
25
26 private string Translate(Type type)
27 {
28
29 if (type == null)
30 return "void";
31 if (type == typeof(NullReference))
32 return "null";
33 else if (type == typeof(int))
34 return "int";
35 else if (type == typeof(float))
36 return "float";
37 else if (type == typeof(double))
38 return "double";
39 else if (type == typeof(bool))
40 return "boolean";
41 else if (type == typeof(string))
42 return "string";
43 else if (type == typeof(ArrayList))
44 return "array";
45 else if (type == typeof(char))
46 return "char";
47 else
48 throw new ParserException("Type '" + type.Name + "' not supported by type inference system.");
49 }
50
51 #endregion;
52
53 #region Public Methods
54
55 public Derivation()
56 {
57
58 if (derivates != null)
59 return;
60
61 derivates = new Dictionary<TokenType, Dictionary<String, Type>>();
62
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);
69 Type typeArray = typeof(ArrayList);
70 Type typeChar = typeof(char);
71
72 Dictionary<string, Type> Logic = new Dictionary<string, Type>();
73
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;
79
80 derivates[TokenType.And] = Logic;
81 derivates[TokenType.Or] = Logic;
82
83 Dictionary<string, Type> Comparision = new Dictionary<string, Type>();
84
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;
93
94 Comparision[Derivate(typeNull, null)] = typeBoolean;
95 Comparision[Derivate(typeNull, typeNull)] = typeBoolean;
96
97 Comparision[Derivate(typeInt, null)] = typeBoolean;
98 Comparision[Derivate(typeInt, typeInt)] = typeBoolean;
99 Comparision[Derivate(typeInt, typeFloat)] = typeBoolean;
100 Comparision[Derivate(typeInt, typeDouble)] = typeBoolean;
101
102 Comparision[Derivate(typeFloat, null)] = typeBoolean;
103 Comparision[Derivate(typeFloat, typeInt)] = typeBoolean;
104 Comparision[Derivate(typeFloat, typeFloat)] = typeBoolean;
105 Comparision[Derivate(typeFloat, typeDouble)] = typeBoolean;
106
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;
112
113 Comparision[Derivate(typeBoolean, null)] = typeBoolean;
114 Comparision[Derivate(typeBoolean, typeBoolean)] = typeBoolean;
115
116 Comparision[Derivate(typeString, null)] = typeBoolean;
117 Comparision[Derivate(typeString, typeString)] = typeBoolean;
118 Comparision[Derivate(typeString, typeChar)] = typeBoolean;
119
120 derivates[TokenType.Equal] = Comparision;
121 derivates[TokenType.NotEqual] = Comparision;
122
123 Dictionary<string, Type> Relation = new Dictionary<string, Type>();
124
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;
132
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;
138
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;
144
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;
151
152 Relation[Derivate(typeString, null)] = typeBoolean;
153 Relation[Derivate(typeString, typeString)] = typeBoolean;
154 Relation[Derivate(typeString, typeChar)] = typeBoolean;
155
156 derivates[TokenType.Greater] = Relation;
157 derivates[TokenType.GreaterEqual] = Relation;
158 derivates[TokenType.Less] = Relation;
159 derivates[TokenType.LessEqual] = Relation;
160
161 Dictionary<string, Type> Plus = new Dictionary<string, Type>();
162
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;
171
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;
177
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;
183
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;
192
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;
201
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;
207
208 derivates[TokenType.Plus] = Plus;
209
210 Dictionary<string, Type> Minus = new Dictionary<string, Type>();
211
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;
219
220 Minus[Derivate(typeInt, null)] = null;
221 Minus[Derivate(typeInt, typeInt)] = typeInt;
222 Minus[Derivate(typeInt, typeFloat)] = typeFloat;
223 Minus[Derivate(typeInt, typeChar)] = typeChar;
224
225 Minus[Derivate(typeFloat, null)] = null;
226 Minus[Derivate(typeFloat, typeInt)] = typeFloat;
227 Minus[Derivate(typeFloat, typeFloat)] = typeFloat;
228 Minus[Derivate(typeFloat, typeChar)] = typeFloat;
229
230 Minus[Derivate(typeString, null)] = typeString;
231 Minus[Derivate(typeString, typeString)] = typeString;
232 Minus[Derivate(typeString, typeChar)] = typeString;
233
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;
241
242 Minus[Derivate(typeChar, null)] = typeChar;
243 Minus[Derivate(typeChar, typeInt)] = typeChar;
244 Minus[Derivate(typeChar, typeFloat)] = typeChar;
245 Minus[Derivate(typeChar, typeChar)] = typeChar;
246
247 derivates[TokenType.Minus] = Minus;
248
249 Dictionary<string, Type> Factor = new Dictionary<string, Type>();
250
251 Factor[Derivate(null, null)] = null;
252 Factor[Derivate(null, typeInt)] = null;
253 Factor[Derivate(null, typeFloat)] = typeFloat;
254 Factor[Derivate(null, typeChar)] = typeChar;
255
256 Factor[Derivate(typeInt, null)] = null;
257 Factor[Derivate(typeInt, typeInt)] = typeInt;
258 Factor[Derivate(typeInt, typeFloat)] = typeFloat;
259 Factor[Derivate(typeInt, typeChar)] = typeChar;
260
261 Factor[Derivate(typeFloat, null)] = typeFloat;
262 Factor[Derivate(typeFloat, typeInt)] = typeFloat;
263 Factor[Derivate(typeFloat, typeFloat)] = typeFloat;
264 Factor[Derivate(typeFloat, typeChar)] = typeFloat;
265
266 Factor[Derivate(typeChar, null)] = typeFloat;
267 Factor[Derivate(typeChar, typeInt)] = typeFloat;
268 Factor[Derivate(typeChar, typeFloat)] = typeFloat;
269 Factor[Derivate(typeChar, typeChar)] = typeChar;
270
271 derivates[TokenType.Multiply] = Factor;
272 derivates[TokenType.Divide] = Factor;
273 derivates[TokenType.Modulo] = Factor;
274
275 }
276
284 public Type Derivate(Token token, Type destination, Type source)
285 {
286
287 if (!derivates.ContainsKey(token.Type))
288 throw new ParserException("Token '" + token + "' ist kein binary operator.");
289
290 Dictionary<string, Type> derivate = derivates[token.Type];
291
292 string key = Derivate(destination, source);
293
294 if (!derivate.ContainsKey(key))
295 {
296
297 string dst = Translate(destination);
298 string src = Translate(source);
299
300 throw new ParserException("Die Operation '" + token.Lexeme + "' kann '" + Translate(destination) + "' und '" + Translate(source) + "' nicht ableiten.");
301
302 }
303
304 return derivate[key];
305
306 }
307
308 #endregion
309
310 }
311}
A lexical token or simply token is a string with an assigned and thus identified meaning.
Definition Token.cs:100
TokenType
Known types of Token.
Definition Token.cs:12