X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FInterpreter.cpp;h=8b9b56a2098eb9ae9a6a4002020f4973fdfa6675;hb=3a30342daecdb9ee050ab20fcb50819a599d6343;hp=0c17bd56af986ea20ceb83ecab1cd43ca1db9f17;hpb=9f352d64f920f46a2d5b4fe67408154629933293;p=l2e.git diff --git a/src/loader/Interpreter.cpp b/src/loader/Interpreter.cpp index 0c17bd5..8b9b56a 100644 --- a/src/loader/Interpreter.cpp +++ b/src/loader/Interpreter.cpp @@ -54,18 +54,9 @@ using std::vector; namespace loader { Interpreter::~Interpreter() { - for (vector::const_iterator i(postponedDefinitions.begin()), end(postponedDefinitions.end()); i != end; ++i) { - delete i->identifier; - } for (std::map::const_iterator i(imageCache.begin()), end(imageCache.end()); i != end; ++i) { SDL_FreeSurface(i->second); } - // TODO: maybe need to reverse the array deletion check if most objects turn out to be arrays (of char) - for (std::map >::const_iterator i(values.begin()), end(values.end()); i != end; ++i) { - for (vector::const_iterator j(i->second.begin()), end(i->second.end()); j != end; ++j) { - delete[] reinterpret_cast(*j); - } - } } @@ -120,7 +111,7 @@ void Interpreter::ReadLiteral(const Definition &dfn) { (dfn.GetLiteral()->GetType() == Literal::PATH || dfn.GetLiteral()->GetType() == Literal::STRING) ? dfn.GetLiteral()->GetString().size() : td.Size()); - char *object(new char[size]); + char *object(alloc.Alloc(size)); if (dfn.GetLiteral()->GetType() == Literal::OBJECT) { ReadObject(typeId, id, object, *dfn.GetLiteral()->GetProperties()); } else { @@ -151,6 +142,8 @@ void Interpreter::ReadLiteral(int typeId, int id, char *object, const Literal &l std::memcpy(object, literal.GetString().c_str(), literal.GetString().size()); object[literal.GetString().size()] = '\0'; break; + case Literal::SCRIPT: + throw Error("script compiler not implemented"); case Literal::STRING: std::memcpy(object, literal.GetString().c_str(), literal.GetString().size()); object[literal.GetString().size()] = '\0'; @@ -160,7 +153,6 @@ void Interpreter::ReadLiteral(int typeId, int id, char *object, const Literal &l break; case Literal::OBJECT: throw Error("illogical branch: read literal object as non-object literal"); - break; } } @@ -170,7 +162,7 @@ void *Interpreter::GetObject(int typeId, const Value &v) { if (v.GetLiteral().IsObject()) { int typeId(TypeDescription::GetTypeId(v.GetLiteral().GetTypeName())); const TypeDescription &td(TypeDescription::Get(typeId)); - char *object(new char[td.Size()]); + char *object(alloc.Alloc(td.Size())); td.Construct(object); int id(values[typeId].size()); values[typeId].push_back(object); @@ -190,7 +182,7 @@ void *Interpreter::GetObject(int typeId, const Value &v) { typeId = TypeDescription::GetTypeId("Boolean"); id = values[typeId].size(); const TypeDescription &td(TypeDescription::Get(typeId)); - char *object(new char[td.Size()]); + char *object(alloc.Alloc(td.Size())); values[typeId].push_back(new (object) bool(v.GetLiteral().GetBoolean())); } break; @@ -199,7 +191,7 @@ void *Interpreter::GetObject(int typeId, const Value &v) { typeId = TypeDescription::GetTypeId("Color"); id = values[typeId].size(); const TypeDescription &td(TypeDescription::Get(typeId)); - char *object(new char[td.Size()]); + char *object(alloc.Alloc(td.Size())); values[typeId].push_back(new (object) Color(v.GetLiteral().GetRed(), v.GetLiteral().GetGreen(), v.GetLiteral().GetBlue(), v.GetLiteral().GetAlpha())); } break; @@ -208,7 +200,7 @@ void *Interpreter::GetObject(int typeId, const Value &v) { typeId = TypeDescription::GetTypeId("Number"); id = values[typeId].size(); const TypeDescription &td(TypeDescription::Get(typeId)); - char *object(new char[td.Size()]); + char *object(alloc.Alloc(td.Size())); values[typeId].push_back(new (object) int(v.GetLiteral().GetNumber())); } break; @@ -216,17 +208,19 @@ void *Interpreter::GetObject(int typeId, const Value &v) { { typeId = TypeDescription::GetTypeId("Path"); id = values[typeId].size(); - char *str(new char[v.GetLiteral().GetString().size() + 1]); + char *str(alloc.Alloc(v.GetLiteral().GetString().size() + 1)); std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size()); str[v.GetLiteral().GetString().size()] = '\0'; values[typeId].push_back(str); } break; + case Literal::SCRIPT: + throw Error("script compiler not implemented"); case Literal::STRING: { typeId = TypeDescription::GetTypeId("String"); id = values[typeId].size(); - char *str(new char[v.GetLiteral().GetString().size() + 1]); + char *str(alloc.Alloc(v.GetLiteral().GetString().size() + 1)); std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size()); str[v.GetLiteral().GetString().size()] = '\0'; values[typeId].push_back(str); @@ -237,7 +231,7 @@ void *Interpreter::GetObject(int typeId, const Value &v) { typeId = TypeDescription::GetTypeId("Vector"); id = values[typeId].size(); const TypeDescription &td(TypeDescription::Get(typeId)); - char *object(new char[td.Size()]); + char *object(alloc.Alloc(td.Size())); values[typeId].push_back(new (object) Vector(v.GetLiteral().GetX(), v.GetLiteral().GetY())); } break; @@ -246,7 +240,7 @@ void *Interpreter::GetObject(int typeId, const Value &v) { typeId = TypeDescription::GetTypeId(v.GetLiteral().GetTypeName()); const TypeDescription &td(TypeDescription::Get(typeId)); id = values[typeId].size(); - char *object(new char[td.Size()]); + char *object(alloc.Alloc(td.Size())); td.Construct(object); ReadObject(typeId, id, object, *v.GetLiteral().GetProperties()); } @@ -265,7 +259,7 @@ void Interpreter::ReadObject(const Definition &dfn) { int typeId(TypeDescription::GetTypeId(dfn.TypeName())); const TypeDescription &td(TypeDescription::Get(typeId)); int id(values[typeId].size()); - char *object(new char[td.Size()]); + char *object(alloc.Alloc(td.Size())); td.Construct(object); values[typeId].push_back(object); ReadObject(typeId, id, object, *dfn.GetProperties()); @@ -282,7 +276,7 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis char *dest(object + fd.Offset()); if (fd.IsAggregate()) { int arraySize(i->second->GetLiteral().ArraySize()); - char *aggregate(new char[fieldType.Size() * arraySize]); + char *aggregate(alloc.Alloc(fieldType.Size() * arraySize)); char *iter(aggregate); if (i->second->GetLiteral().GetType() == Literal::ARRAY_PROPS) { const vector &list(i->second->GetLiteral().GetPropertyLists()); @@ -304,6 +298,13 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis } else { throw Error("aggregate type fields must be referenced"); } + } else if (i->second->IsLiteral() && !fd.IsReferenced()) { + // inline literals + if (i->second->GetLiteral().IsObject()) { + ReadObject(fd.TypeId(), -1, dest, *i->second->GetLiteral().GetProperties()); + } else { + ReadLiteral(fd.TypeId(), -1, dest, i->second->GetLiteral()); + } } else { char *src(reinterpret_cast(GetObject(fd.TypeId(), *i->second))); if (fd.TypeId() == TypeDescription::GetTypeId("Image")) { @@ -340,7 +341,7 @@ bool Interpreter::CanLink(const Value &v) const { } void Interpreter::Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType, bool inlined) { - char *str(new char[identifier.size() + 1]); + char *str(alloc.Alloc(identifier.size() + 1)); std::memcpy(str, identifier.c_str(), identifier.size()); str[identifier.size()] = '\0'; postponedDefinitions.push_back(PostponedDefinition(type, id, offset, str, linkedType, inlined)); @@ -350,32 +351,42 @@ void Interpreter::Postpone(int type, int id, std::ptrdiff_t offset, const std::s void Interpreter::CreateTypeDescriptions() { { TypeDescription &td(TypeDescription::CreateOrGet("Boolean")); + td.SetDescription("Logical value which can be either true or false."); td.SetSize(sizeof(bool)); } { TypeDescription &td(TypeDescription::CreateOrGet("Color")); + td.SetDescription( + "A color in RGB format with an optional alpha channel.\n" + "Components range from 0 to 255.\n" + "Alpha defaults to 255 if omitted."); td.SetSize(sizeof(Color)); } { TypeDescription &td(TypeDescription::CreateOrGet("Image")); + td.SetDescription("Path to a PNG file with image data."); td.SetSize(sizeof(SDL_Surface)); } { TypeDescription &td(TypeDescription::CreateOrGet("Number")); + td.SetDescription("A signed integer."); td.SetSize(sizeof(int)); } { int stringId(TypeDescription::GetTypeId("String")); TypeDescription &td(TypeDescription::CreateOrGet("Path")); + td.SetDescription("A path in the filesystem which is interpreted relative to the source file's location."); td.SetSize(1); td.AddSupertype(stringId, 0); } { TypeDescription &td(TypeDescription::CreateOrGet("String")); + td.SetDescription("Some characters."); td.SetSize(1); } { TypeDescription &td(TypeDescription::CreateOrGet("Vector")); + td.SetDescription("A pair of numbers usually describing a 2D translation or offset."); td.SetSize(sizeof(Vector)); } }