X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FInterpreter.h;h=1a73d2dce69c54591354a2ae09fdd9234d03df91;hb=ab063a95735fdb90e7c610015e9155283ba5eca6;hp=e72d4da202d43a99ae0bf8a1490c49ff7570ffbf;hpb=1aa61eca26f5b4a4cb856a39e802f5899548b691;p=l2e.git diff --git a/src/loader/Interpreter.h b/src/loader/Interpreter.h index e72d4da..1a73d2d 100644 --- a/src/loader/Interpreter.h +++ b/src/loader/Interpreter.h @@ -1,225 +1,192 @@ -/* - * Interpreter.h - * - * Created on: Aug 26, 2012 - * Author: holy - */ - #ifndef LOADER_INTERPRETER_H_ #define LOADER_INTERPRETER_H_ -#include "../geometry/Vector.h" -#include "../graphics/Color.h" -#include "../graphics/ComplexAnimation.h" +#include "PagedAllocator.h" +#include "ParsedSource.h" +#include "../common/Script.h" #include +#include #include #include #include #include -namespace battle { - class Hero; - class Monster; - class PartyLayout; - class Stats; -} - -namespace common { - class Ikari; - class Item; - class Spell; - class TargetingMode; -} - -namespace graphics { - class Animation; - class Font; - class Frame; - class Gauge; - struct MenuProperties; - class SimpleAnimation; - class Sprite; -} - namespace loader { -class Definition; -class ParsedSource; -class PropertyList; -class Value; - class Interpreter { public: + static const int BOOLEAN_ID = 1; + static const int COLOR_ID = 2; + static const int IMAGE_ID = 3; + static const int NUMBER_ID = 4; + static const int PATH_ID = 5; + static const int SCRIPT_ID = 6; + static const int STRING_ID = 7; + static const int VECTOR_ID = 8; + + /// Instances of this class are thrown if an + /// interpretation error is encounteres such as a + /// reference to an undeclared or inlining of an undefined + /// object. class Error: public std::runtime_error { public: - Error(const std::string &msg) : std::runtime_error("interpreter error: " + msg) { } + Error(const std::string &msg) + : std::runtime_error("interpreter error: " + msg) { } }; public: - Interpreter(const ParsedSource &source) : source(source) { } + /// Create an interpreter that rads from given parsed + /// source. + /// After creation you should call ReadSource() before + /// querying e.g. with GetObject(). + explicit Interpreter(const ParsedSource &source) + : source(source), alloc(4096) { } ~Interpreter(); private: Interpreter(const Interpreter &); Interpreter &operator =(const Interpreter &); public: + /// Interpret all exported definitions of the underlying + /// source file. void ReadSource(); + /// Get an object of given type that is defined by given + /// name. + /// If the object identified by name is of another type, + /// a cast is performed for compatible types. + /// Error is thrown if the object is undefined or a cast + /// was unsuccessful. + void *GetObject(int typeId, const std::string &name); + + /// Register the type descriptions of builtin types. + /// This should be called before any interpretation (like + /// calling ReadSource()) is performed. + static void CreateTypeDescriptions(); + /// Represents a parsed definition with resulting type and + /// object IDs. + struct ParsedDefinition { + ParsedDefinition(const Definition *dfn, int type, int id) + : dfn(dfn), type(type), id(id) { } + const Definition *dfn; + int type; + int id; + }; + /// Represents a postponed object where the type and + /// target location are already known, but the definition + /// was not accessible at interpretation time. + /// It could become available later or be dynamically + /// linked by its identifier. + struct PostponedDefinition { + PostponedDefinition( + char *dest, + const char *identifier, + int type, + bool inlined, + bool aggregate) + : dest(dest) + , identifier(identifier) + , type(type) + , inlined(inlined) + , aggregate(aggregate) { } + char *dest; + const char *identifier; + int type; + bool inlined; + bool aggregate; + }; -public: - graphics::Animation *GetAnimation(const std::string &name); - bool GetBoolean(const std::string &name) const; - const graphics::Color &GetColor(const std::string &name) const; - graphics::Font *GetFont(const std::string &name); - graphics::Frame *GetFrame(const std::string &name); - graphics::Gauge *GetGauge(const std::string &name); - battle::Hero *GetHero(const std::string &name); - common::Ikari *GetIkari(const std::string &name); - common::Item *GetItem(const std::string &name); - graphics::MenuProperties *GetMenuProperties(const std::string &name); - battle::Monster *GetMonster(const std::string &name); - int GetNumber(const std::string &name) const; - battle::PartyLayout *GetPartyLayout(const std::string &name); - const char *GetPath(const std::string &name) const; - common::Spell *GetSpell(const std::string &name); - graphics::Sprite *GetSprite(const std::string &name); - const char *GetString(const std::string &name) const; - common::TargetingMode *GetTargetingMode(const std::string &name); - geometry::Vector GetVector(const std::string &name) const; - -public: - const std::vector &Booleans() const { return booleans; } - const std::vector &Colors() const { return colors; } - const std::vector &ComplexAnimations() const { return complexAnimations; } - const std::vector &Fonts() const { return fonts; } - const std::vector &Frames() const { return frames; } - const std::vector &Gauges() const { return gauges; } - const std::vector &Heroes() const { return heroes; } - const std::vector &Ikaris() const { return ikaris; } - const std::vector &Images() const { return images; } - const std::vector &Items() const { return items; } - const std::vector &MenuProperties() const { return menuProperties; } - const std::vector &Monsters() const { return monsters; } - const std::vector &Numbers() const { return numbers; } - const std::vector &PartyLayouts() const { return partyLayouts; } - const std::vector &SimpleAnimations() const { return simpleAnimations; } - const std::vector &Spells() const { return spells; } - const std::vector &Sprites() const { return sprites; } - const std::vector &Strings() const { return strings; } - const std::vector &TargetingModes() const { return targetingModes; } - const std::vector > &Vectors() const { return vectors; } + /// Get all the identifiers that are marked for export + /// by name in the source. + const std::set &ExportedIdentifiers() const { + return source.Exports(); + } + /// Get the object definition for given identifier. + const ParsedDefinition &GetDefinition(const std::string &identifier); + /// Get all images reference by he source. + /// The returned map is indexed by filenames relative to + /// the source file and contains ready-to-blit surfaces. + const std::map &Images() const { + return imageCache; + } + /// Get all definitions that were postponed because they + /// were not in the parsed source. + const std::vector &PostponedDefinitions() const { + return postponedDefinitions; + } + /// A map of object id to object. + const std::map > &Values() const { + return values; + } private: + /// Interpret given definition. void ReadDefinition(const Definition &); + /// Interpret given definition as a literal. void ReadLiteral(const Definition &); + /// Interpret given definition as a complex object. void ReadObject(const Definition &); - graphics::Animation *GetAnimation(const Value &); - graphics::Color GetColor(const Value &); - bool GetBoolean(const Value &); - graphics::Font *GetFont(const Value &); - graphics::Frame *GetFrame(const Value &); - graphics::Gauge *GetGauge(const Value &); - battle::Hero *GetHero(const Value &); - common::Ikari *GetIkari(const Value &); - SDL_Surface *GetImage(const Value &); - common::Item *GetItem(const Value &); - graphics::MenuProperties *GetMenuProperties(const Value &); - battle::Monster *GetMonster(const Value &); - int GetNumber(const Value &); - battle::PartyLayout *GetPartyLayout(const Value &); - const PropertyList *GetPropertyList(const Value &); - const std::vector &GetPropertyListArray(const Value &); - const char *GetPath(const Value &); - common::Spell *GetSpell(const Value &); - graphics::Sprite *GetSprite(const Value &); - const char *GetString(const Value &); - common::TargetingMode *GetTargetingMode(const Value &); - const std::vector &GetValueArray(const Value &); - geometry::Vector GetVector(const Value &); - - void ReadComplexAnimation(graphics::ComplexAnimation &, const PropertyList &); - void ReadComplexAnimationFrame(graphics::ComplexAnimation::FrameProp &, const PropertyList &); - void ReadFont(graphics::Font &, const PropertyList &); - void ReadFrame(graphics::Frame &, const PropertyList &); - void ReadGauge(graphics::Gauge &, const PropertyList &); - void ReadHero(battle::Hero &, const PropertyList &); - void ReadIkari(common::Ikari &, const PropertyList &); - void ReadItem(common::Item &, const PropertyList &); - void ReadMenuProperties(graphics::MenuProperties &, const PropertyList &); - void ReadMonster(battle::Monster &, const PropertyList &); - void ReadPartyLayout(battle::PartyLayout &, const PropertyList &); - void ReadSimpleAnimation(graphics::SimpleAnimation &, const PropertyList &); - void ReadSpell(common::Spell &, const PropertyList &); - void ReadSprite(graphics::Sprite &, const PropertyList &); - void ReadStats(battle::Stats &, const PropertyList &); - void ReadTargetingMode(common::TargetingMode &, const PropertyList &); + /// Interpret given literal as an object of given type. + /// The resulting object shall be accessible by given ID + /// (whiich is already reserved) and be written to given + /// destination. + void ReadLiteral(int typeId, int id, char *dest, const Literal &); + /// Get an object of given type from given value. + /// The object will be registere as new and be given a + /// unique ID. + /// Any nested objects will be created likewise. + void *GetObject(int typeId, const Value &value); + /// Read an object of given type and properties into dest. + /// The object should alrady have an assigned identifier. + /// Pass -1 as ID if the object is anonymous. + /// Note that anonymous objects must not contain + /// references as they are excluded from linking. + void ReadObject(int typeId, int id, char *dest, const PropertyList &); + /// The function that does the actual compiling when you + /// call ReadScript(const vector &). + void ReadScript(const std::vector &, common::Script *); + /// Compile a tokenized script. + /// Storage for the script will be allocated as neccessary. + char *ReadScript(const std::vector &); + /// Reinterpret dest as a script code and return a + /// reference to that. + common::Script::Code &CreateScriptCode(common::Script::Command c, char *dest); + /// Write address to dest. + void ReadScriptAddress(const ScriptToken &t, char *dest); + /// Write integer to est. + void ReadScriptInteger(const ScriptToken &t, char *dest); + /// Write vector to dest. + void ReadScriptVector(const ScriptToken &t, char *dest); + + /// Get a surface from given image path. + /// Load it if neccessary. + SDL_Surface *GetImage(const std::string &); + + /// Check if a value can be linked right now or if linkage + /// must be deferred (with Postpone()). + bool CanLink(const Value &) const; + /// Defer linking of given object reference. + void Postpone( + char *dest, + const std::string &identifier, + int type, + bool inlined = true, + bool aggregate = false); private: const ParsedSource &source; - enum Type { - BOOLEAN, - COLOR, - COMPLEX_ANIMATION, - FONT, - FRAME, - GAUGE, - HERO, - IKARI, - IMAGE, - ITEM, - MENU_PROPERTIES, - MONSTER, - NUMBER, - PARTY_LAYOUT, - PATH, - PROPERTY_LIST_ARRAY, - SIMPLE_ANIMATION, - SPELL, - SPRITE, - STRING, - TARGETING_MODE, - VECTOR, - VALUE_ARRAY, - }; - struct ParsedDefinition { - ParsedDefinition(const Definition *dfn, Type type, int index) - : dfn(dfn), type(type), index(index) { } - const Definition *dfn; - Type type; - int index; - }; - std::map parsedDefinitions; - std::map imageCache; + PagedAllocator alloc; - std::vector booleans; - std::vector colors; - std::vector complexAnimations; - std::vector fonts; - std::vector frames; - std::vector gauges; - std::vector heroes; - std::vector ikaris; - std::vector images; - std::vector items; - std::vector menuProperties; - std::vector monsters; - std::vector numbers; - std::vector partyLayouts; - std::vector propertyLists; - std::vector > propertyListArrays; - std::vector simpleAnimations; - std::vector spells; - std::vector sprites; - std::vector strings; - std::vector targetingModes; - std::vector > valueArrays; - std::vector > vectors; + std::map parsedDefinitions; + std::vector postponedDefinitions; + std::map imageCache; + std::map > values; }; } -#endif /* LOADER_INTERPRETER_H_ */ +#endif