X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FParsedSource.cpp;h=bceed089f0377f4a4678edc84414afa4d7c3084b;hb=09e8cfd4d7b2d187fed0870ebdb2e9e3f77fe4b9;hp=ec263042a68cdba8d7c9c27e43479ae9fee1455f;hpb=32b5ea1b0f05283eb588b2b069d667f7c36e84da;p=l2e.git diff --git a/src/loader/ParsedSource.cpp b/src/loader/ParsedSource.cpp index ec26304..bceed08 100644 --- a/src/loader/ParsedSource.cpp +++ b/src/loader/ParsedSource.cpp @@ -7,6 +7,8 @@ #include "ParsedSource.h" +#include "utility.h" + #include #include @@ -17,6 +19,12 @@ using std::vector; namespace loader { +ParsedSource::~ParsedSource() { + for (map::const_iterator i(declarations.begin()), end(declarations.end()); i != end; ++i) { + delete i->second; + } +} + void ParsedSource::AddDeclaration(Declaration *d) { map::iterator i(declarations.find(d->Identifier())); if (i != declarations.end()) { @@ -51,6 +59,59 @@ void ParsedSource::ExportIdentifier(const std::string &i) { } +bool ParsedSource::IsDeclared(const std::string &name) const { + return declarations.count(name); +} + +Declaration &ParsedSource::GetDeclaration(const std::string &name) { + map::const_iterator i(declarations.find(name)); + if (i != declarations.end()) { + return *i->second; + } else { + throw runtime_error("undeclared identifier " + name); + } +} + +const Declaration &ParsedSource::GetDeclaration(const std::string &name) const { + map::const_iterator i(declarations.find(name)); + if (i != declarations.end()) { + return *i->second; + } else { + throw runtime_error("undeclared identifier " + name); + } +} + +bool ParsedSource::IsDefined(const std::string &name) const { + return definitions.count(name); +} + +Definition &ParsedSource::GetDefinition(const std::string &name) { + map::const_iterator i(definitions.find(name)); + if (i != definitions.end()) { + return *i->second; + } else { + throw runtime_error("undefined identifier " + name); + } +} + +const Definition &ParsedSource::GetDefinition(const std::string &name) const { + map::const_iterator i(definitions.find(name)); + if (i != definitions.end()) { + return *i->second; + } else { + throw runtime_error("undefined identifier " + name); + } +} + + +Definition::~Definition() { + if (isLiteral) { + delete reinterpret_cast(value); + } else { + delete reinterpret_cast(value); + } +} + void Definition::SetValue(Literal *v) { value = v; isLiteral = true; @@ -63,7 +124,7 @@ void Definition::SetValue(PropertyList *v) { Literal *Definition::GetLiteral() { if (isLiteral) { - return (Literal *)value; + return reinterpret_cast(value); } else { throw runtime_error("tried to access properties as literal"); } @@ -71,7 +132,7 @@ Literal *Definition::GetLiteral() { const Literal *Definition::GetLiteral() const { if (isLiteral) { - return (Literal *)value; + return reinterpret_cast(value); } else { throw runtime_error("tried to access properties as literal"); } @@ -79,7 +140,7 @@ const Literal *Definition::GetLiteral() const { PropertyList *Definition::GetProperties() { if (!isLiteral) { - return (PropertyList *)value; + return reinterpret_cast(value); } else { throw runtime_error("tried to access literal value as property list"); } @@ -87,7 +148,9 @@ PropertyList *Definition::GetProperties() { const PropertyList *Definition::GetProperties() const { if (!isLiteral) { - return (PropertyList *)value; + return reinterpret_cast(value); + } else if (GetLiteral()->GetType() == Literal::OBJECT) { + return GetLiteral()->GetProperties(); } else { throw runtime_error("tried to access literal value as property list"); } @@ -148,6 +211,16 @@ Literal::Literal(int number) } +Literal::Literal(const string &dir, const string &path) +: props(0) +, str(CatPath(dir, path)) +, i1(0), i2(0) +, i3(0), i4(0) +, b(false) +, type(STRING) { + +} + Literal::Literal(const string &str) : props(0) , str(str) @@ -177,6 +250,26 @@ Literal::Literal(const string &typeName, PropertyList *properties) } +Literal::~Literal() { + switch (type) { + case ARRAY_VALUES: + for (vector::const_iterator i(values.begin()), end(values.end()); i != end; ++i) { + delete *i; + } + break; + case ARRAY_PROPS: + for (vector::const_iterator i(propertyLists.begin()), end(propertyLists.end()); i != end; ++i) { + delete *i; + } + break; + case OBJECT: + delete props; + break; + default: + break; + } +} + const vector &Literal::GetValues() const { if (type == ARRAY_VALUES) { @@ -282,6 +375,29 @@ const PropertyList *Literal::GetProperties() const { } } + +Value::~Value() { + if (isLiteral) { + delete literal; + } +} + +const Literal &Value::GetLiteral() const { + if (isLiteral) { + return *literal; + } else { + throw runtime_error("tried to access literal of identifier value"); + } +} + +const std::string &Value::GetIdentifier() const { + if (!isLiteral) { + return identifier; + } else { + throw runtime_error("tried to access identifier of literal value"); + } +} + } @@ -324,6 +440,9 @@ ostream &operator <<(ostream &out, const loader::Literal &l) { case loader::Literal::NUMBER: out << "number, " << l.GetNumber(); break; + case loader::Literal::PATH: + out << "path, \"" << l.GetString() << '"'; + break; case loader::Literal::STRING: out << "string, \"" << l.GetString() << '"'; break;