]> git.localhorst.tv Git - l2e.git/blob - src/loader/ParsedSource.cpp
added more getters to ParsedSource
[l2e.git] / src / loader / ParsedSource.cpp
1 /*
2  * ParsedSource.cpp
3  *
4  *  Created on: Aug 26, 2012
5  *      Author: holy
6  */
7
8 #include "ParsedSource.h"
9
10 #include <ostream>
11 #include <stdexcept>
12
13 using std::map;
14 using std::runtime_error;
15 using std::string;
16 using std::vector;
17
18 namespace loader {
19
20 void ParsedSource::AddDeclaration(Declaration *d) {
21         map<string, Declaration *>::iterator i(declarations.find(d->Identifier()));
22         if (i != declarations.end()) {
23                 if (d->TypeName() != i->second->TypeName()) {
24                         throw runtime_error("invalid redeclaration of " + i->second->TypeName() + " " + d->Identifier());
25                 }
26         } else {
27                 declarations.insert(std::make_pair(d->Identifier(), d));
28         }
29 }
30
31 void ParsedSource::AddDefinition(Definition *d) {
32         map<string, Definition *>::iterator i(definitions.find(d->Identifier()));
33         if (i != definitions.end()) {
34                 throw runtime_error("redefinition of " + i->second->TypeName() + " " + d->Identifier());
35         } else {
36                 definitions.insert(std::make_pair(d->Identifier(), d));
37         }
38 }
39
40 void ParsedSource::ExportDeclaration(Declaration *d) {
41         AddDeclaration(d);
42         exports.insert(d->Identifier());
43 }
44
45 void ParsedSource::ExportIdentifier(const std::string &i) {
46         if (declarations.count(i)) {
47                 exports.insert(i);
48         } else {
49                 throw runtime_error("cannot export undeclared identifier " + i);
50         }
51 }
52
53
54 bool ParsedSource::IsDeclared(const std::string &name) const {
55         return declarations.count(name);
56 }
57
58 Declaration &ParsedSource::GetDeclaration(const std::string &name) {
59         map<string, Declaration *>::const_iterator i(declarations.find(name));
60         if (i != declarations.end()) {
61                 return *i->second;
62         } else {
63                 throw runtime_error("undeclared identifier " + name);
64         }
65 }
66
67 const Declaration &ParsedSource::GetDeclaration(const std::string &name) const {
68         map<string, Declaration *>::const_iterator i(declarations.find(name));
69         if (i != declarations.end()) {
70                 return *i->second;
71         } else {
72                 throw runtime_error("undeclared identifier " + name);
73         }
74 }
75
76 bool ParsedSource::IsDefined(const std::string &name) const {
77         return definitions.count(name);
78 }
79
80 Definition &ParsedSource::GetDefinition(const std::string &name) {
81         map<string, Definition *>::const_iterator i(definitions.find(name));
82         if (i != definitions.end()) {
83                 return *i->second;
84         } else {
85                 throw runtime_error("undefined identifier " + name);
86         }
87 }
88
89 const Definition &ParsedSource::GetDefinition(const std::string &name) const {
90         map<string, Definition *>::const_iterator i(definitions.find(name));
91         if (i != definitions.end()) {
92                 return *i->second;
93         } else {
94                 throw runtime_error("undefined identifier " + name);
95         }
96 }
97
98
99 void Definition::SetValue(Literal *v) {
100         value = v;
101         isLiteral = true;
102 }
103
104 void Definition::SetValue(PropertyList *v) {
105         value = v;
106         isLiteral = false;
107 }
108
109 Literal *Definition::GetLiteral() {
110         if (isLiteral) {
111                 return (Literal *)value;
112         } else {
113                 throw runtime_error("tried to access properties as literal");
114         }
115 }
116
117 const Literal *Definition::GetLiteral() const {
118         if (isLiteral) {
119                 return (Literal *)value;
120         } else {
121                 throw runtime_error("tried to access properties as literal");
122         }
123 }
124
125 PropertyList *Definition::GetProperties() {
126         if (!isLiteral) {
127                 return (PropertyList *)value;
128         } else {
129                 throw runtime_error("tried to access literal value as property list");
130         }
131 }
132
133 const PropertyList *Definition::GetProperties() const {
134         if (!isLiteral) {
135                 return (PropertyList *)value;
136         } else {
137                 throw runtime_error("tried to access literal value as property list");
138         }
139 }
140
141
142 PropertyList::~PropertyList() {
143         for (map<string, Value *>::iterator i(props.begin()), end(props.end()); i != end; ++i) {
144                 delete i->second;
145         }
146 }
147
148
149 Literal::Literal(const vector<Value *> &v)
150 : props(0)
151 , values(v)
152 , i1(0), i2(0)
153 , i3(0), i4(0)
154 , b(false)
155 , type(ARRAY_VALUES) {
156
157 }
158
159 Literal::Literal(const std::vector<PropertyList *> &pls)
160 : props(0)
161 , propertyLists(pls)
162 , i1(0), i2(0)
163 , i3(0), i4(0)
164 , b(false)
165 , type(ARRAY_PROPS) {
166
167 }
168
169 Literal::Literal(bool b)
170 : props(0)
171 , i1(0), i2(0)
172 , i3(0), i4(0)
173 , b(b)
174 , type(BOOLEAN) {
175
176 }
177
178 Literal::Literal(int r, int g, int b, int a)
179 : props(0)
180 , i1(r), i2(g)
181 , i3(b), i4(a)
182 , b(false)
183 , type(COLOR) {
184
185 }
186
187 Literal::Literal(int number)
188 : props(0)
189 , i1(number), i2(0)
190 , i3(0), i4(0)
191 , b(false)
192 , type(NUMBER) {
193
194 }
195
196 Literal::Literal(const string &str)
197 : props(0)
198 , str(str)
199 , i1(0), i2(0)
200 , i3(0), i4(0)
201 , b(false)
202 , type(STRING) {
203
204 }
205
206 Literal::Literal(int x, int y)
207 : props(0)
208 , i1(x), i2(y)
209 , i3(0), i4(0)
210 , b(false)
211 , type(VECTOR) {
212
213 }
214
215 Literal::Literal(const string &typeName, PropertyList *properties)
216 : props(properties)
217 , str(typeName)
218 , i1(0), i2(0)
219 , i3(0), i4(0)
220 , b(false)
221 , type(OBJECT) {
222
223 }
224
225
226 const vector<Value *> &Literal::GetValues() const {
227         if (type == ARRAY_VALUES) {
228                 return values;
229         } else {
230                 throw runtime_error("tried to access values of non-array literal");
231         }
232 }
233
234 const vector<PropertyList *> &Literal::GetPropertyLists() const {
235         if (type == ARRAY_PROPS) {
236                 return propertyLists;
237         } else {
238                 throw runtime_error("tried to access property lists of non-array literal");
239         }
240 }
241
242 bool Literal::GetBoolean() const {
243         if (type == BOOLEAN) {
244                 return b;
245         } else {
246                 throw runtime_error("tried to access boolean value of non-boolean literal");
247         }
248 }
249
250 int Literal::GetRed() const {
251         if (type == COLOR) {
252                 return i1;
253         } else {
254                 throw runtime_error("tried to access red component of non-color literal");
255         }
256 }
257
258 int Literal::GetGreen() const {
259         if (type == COLOR) {
260                 return i2;
261         } else {
262                 throw runtime_error("tried to access green component of non-color literal");
263         }
264 }
265
266 int Literal::GetBlue() const {
267         if (type == COLOR) {
268                 return i3;
269         } else {
270                 throw runtime_error("tried to access blue component of non-color literal");
271         }
272 }
273
274 int Literal::GetAlpha() const {
275         if (type == COLOR) {
276                 return i4;
277         } else {
278                 throw runtime_error("tried to access alpha component of non-color literal");
279         }
280 }
281
282 int Literal::GetNumber() const {
283         if (type == NUMBER) {
284                 return i1;
285         } else {
286                 throw runtime_error("tried to access numerical value of non-number literal");
287         }
288 }
289
290 const string &Literal::GetString() const {
291         if (type == STRING) {
292                 return str;
293         } else {
294                 throw runtime_error("tried to access string value of non-color literal");
295         }
296 }
297
298 int Literal::GetX() const {
299         if (type == VECTOR) {
300                 return i1;
301         } else {
302                 throw runtime_error("tried to access x component of non-vector literal");
303         }
304 }
305
306 int Literal::GetY() const {
307         if (type == VECTOR) {
308                 return i2;
309         } else {
310                 throw runtime_error("tried to access y component of non-vector literal");
311         }
312 }
313
314 const string &Literal::GetTypeName() const {
315         if (type == OBJECT) {
316                 return str;
317         } else {
318                 throw runtime_error("tried to access type name of non-object literal");
319         }
320 }
321
322 const PropertyList *Literal::GetProperties() const {
323         if (type == OBJECT) {
324                 return props;
325         } else {
326                 throw runtime_error("tried to access properties of non-object literal");
327         }
328 }
329
330 }
331
332
333 namespace std {
334
335 ostream &operator <<(ostream &out, const loader::ParsedSource &source) {
336         out << "parsed source file" << endl;
337         out << "declared objects: " << endl;
338         for (map<string, loader::Declaration *>::const_iterator i(source.Declarations().begin()), end(source.Declarations().end()); i != end; ++i) {
339                 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
340         }
341         out << "defined objects: " << endl;
342         for (map<string, loader::Definition *>::const_iterator i(source.Definitions().begin()), end(source.Definitions().end()); i != end; ++i) {
343                 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
344                 if (i->second->HasLiteralValue()) {
345                         out << "     literal value: " << *i->second->GetLiteral() << endl;
346                 }
347         }
348         out << "exported objects: " << endl;
349         for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
350                 out << " - " << *i << endl;
351         }
352         return out;
353 }
354
355 ostream &operator <<(ostream &out, const loader::Literal &l) {
356         switch (l.GetType()) {
357                 case loader::Literal::ARRAY_VALUES:
358                         out << "array of values";
359                         break;
360                 case loader::Literal::ARRAY_PROPS:
361                         out << "array of property lists";
362                         break;
363                 case loader::Literal::BOOLEAN:
364                         out << "boolean, " << (l.GetBoolean() ? "true" : "false");
365                         break;
366                 case loader::Literal::COLOR:
367                         out << "color, (" << l.GetRed() << ',' << l.GetGreen() << ',' << l.GetBlue() << ',' << l.GetAlpha() << ')';
368                         break;
369                 case loader::Literal::NUMBER:
370                         out << "number, " << l.GetNumber();
371                         break;
372                 case loader::Literal::STRING:
373                         out << "string, \"" << l.GetString() << '"';
374                         break;
375                 case loader::Literal::VECTOR:
376                         out << "vector, <" << l.GetX() << ',' << l.GetY() << '>';
377                         break;
378                 case loader::Literal::OBJECT:
379                         out << "object of type " << l.GetTypeName();
380                         break;
381         }
382         return out;
383 }
384
385 }