]> git.localhorst.tv Git - l2e.git/blob - src/loader/ParsedSource.cpp
added basic parser (not completely tested)
[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::ExportDeclaration(Declaration *d) {
32         AddDeclaration(d);
33         exports.insert(d->Identifier());
34 }
35
36 void ParsedSource::ExportIdentifier(const std::string &i) {
37         if (declarations.count(i)) {
38                 exports.insert(i);
39         } else {
40                 throw runtime_error("cannot export undeclared identifier " + i);
41         }
42 }
43
44
45 void Definition::SetValue(Literal *v) {
46         value = v;
47         isLiteral = true;
48 }
49
50 void Definition::SetValue(PropertyList *v) {
51         value = v;
52         isLiteral = false;
53 }
54
55 Literal *Definition::GetLiteral() {
56         if (isLiteral) {
57                 return (Literal *)value;
58         } else {
59                 throw runtime_error("tried to access properties as literal");
60         }
61 }
62
63 const Literal *Definition::GetLiteral() const {
64         if (isLiteral) {
65                 return (Literal *)value;
66         } else {
67                 throw runtime_error("tried to access properties as literal");
68         }
69 }
70
71 PropertyList *Definition::GetProperties() {
72         if (!isLiteral) {
73                 return (PropertyList *)value;
74         } else {
75                 throw runtime_error("tried to access literal value as property list");
76         }
77 }
78
79 const PropertyList *Definition::GetProperties() const {
80         if (!isLiteral) {
81                 return (PropertyList *)value;
82         } else {
83                 throw runtime_error("tried to access literal value as property list");
84         }
85 }
86
87
88 PropertyList::~PropertyList() {
89         for (map<string, Value *>::iterator i(props.begin()), end(props.end()); i != end; ++i) {
90                 delete i->second;
91         }
92 }
93
94
95 Literal::Literal(const vector<Value *> &v)
96 : props(0)
97 , values(v)
98 , i1(0), i2(0)
99 , i3(0), i4(0)
100 , b(false)
101 , type(ARRAY) {
102
103 }
104
105 Literal::Literal(bool b)
106 : props(0)
107 , i1(0), i2(0)
108 , i3(0), i4(0)
109 , b(b)
110 , type(BOOLEAN) {
111
112 }
113
114 Literal::Literal(int r, int g, int b, int a)
115 : props(0)
116 , i1(r), i2(g)
117 , i3(b), i4(a)
118 , b(false)
119 , type(COLOR) {
120
121 }
122
123 Literal::Literal(const string &str)
124 : props(0)
125 , str(str)
126 , i1(0), i2(0)
127 , i3(0), i4(0)
128 , b(false)
129 , type(STRING) {
130
131 }
132
133 Literal::Literal(int x, int y)
134 : props(0)
135 , i1(x), i2(y)
136 , i3(0), i4(0)
137 , b(false)
138 , type(VECTOR) {
139
140 }
141
142 Literal::Literal(const string &typeName, PropertyList *properties)
143 : props(properties)
144 , str(typeName)
145 , i1(0), i2(0)
146 , i3(0), i4(0)
147 , b(false)
148 , type(OBJECT) {
149
150 }
151
152 }
153
154
155 namespace std {
156
157 ostream &operator <<(ostream &out, const loader::ParsedSource &source) {
158         out << "parsed source file" << endl;
159         out << "declared objects: " << endl;
160         for (map<string, loader::Declaration *>::const_iterator i(source.Declarations().begin()), end(source.Declarations().end()); i != end; ++i) {
161                 out << " - " << i->first << " of type " << i->second->TypeName() << endl;
162         }
163         out << "exported objects: " << endl;
164         for (set<string>::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) {
165                 out << " - " << *i << endl;
166         }
167         return out;
168 }
169
170 }