-/*
- * Interpreter.h
- *
- * Created on: Aug 26, 2012
- * Author: holy
- */
-
#ifndef LOADER_INTERPRETER_H_
#define LOADER_INTERPRETER_H_
-#include "../geometry/Vector.h"
-#include "../graphics/ComplexAnimation.h"
+#include "PagedAllocator.h"
+#include "ParsedSource.h"
+#include "../common/Script.h"
#include <map>
+#include <set>
#include <stdexcept>
#include <string>
#include <vector>
#include <SDL.h>
-namespace battle {
- class Hero;
- class Monster;
- class PartyLayout;
- class Stats;
-}
-
-namespace common {
- class Spell;
- class TargetingMode;
-}
-
-namespace graphics {
- class Animation;
- class Font;
- class Frame;
- class Gauge;
- 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;
+
class Error: public std::runtime_error {
public:
Error(const std::string &msg) : std::runtime_error("interpreter error: " + msg) { }
};
public:
- Interpreter(const ParsedSource &source) : source(source) { }
+ explicit Interpreter(const ParsedSource &source) : source(source), alloc(4096) { }
~Interpreter();
private:
Interpreter(const Interpreter &);
public:
void ReadSource();
+ void *GetObject(int typeId, const std::string &name);
-public:
- graphics::Animation *GetAnimation(const std::string &name);
- bool GetBoolean(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);
- battle::Monster *GetMonster(const std::string &name);
- int GetNumber(const std::string &name) const;
- battle::PartyLayout *GetPartyLayout(const std::string &name);
- 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<int> GetVector(const std::string &name) const;
+ static void CreateTypeDescriptions();
+ struct ParsedDefinition {
+ ParsedDefinition(const Definition *dfn, int type, int id)
+ : dfn(dfn), type(type), id(id) { }
+ const Definition *dfn;
+ int type;
+ int id;
+ };
+ struct PostponedDefinition {
+ PostponedDefinition(int type, int id, std::ptrdiff_t offset, const char *identifier, int linkedType, bool inlined)
+ : type(type), id(id), offset(offset), identifier(identifier), linkedType(linkedType), inlined(inlined) { }
+ int type;
+ int id;
+ std::ptrdiff_t offset;
+ const char *identifier;
+ int linkedType;
+ bool inlined;
+ };
-public:
- const std::vector<bool> &Booleans() const { return booleans; }
- const std::vector<graphics::ComplexAnimation *> &ComplexAnimations() const { return complexAnimations; }
- const std::vector<graphics::Font *> &Fonts() const { return fonts; }
- const std::vector<graphics::Frame *> &Frames() const { return frames; }
- const std::vector<graphics::Gauge *> &Gauges() const { return gauges; }
- const std::vector<battle::Hero *> &Heroes() const { return heroes; }
- const std::vector<SDL_Surface *> &Images() const { return images; }
- const std::vector<battle::Monster *> &Monsters() const { return monsters; }
- const std::vector<int> &Numbers() const { return numbers; }
- const std::vector<battle::PartyLayout *> &PartyLayouts() const { return partyLayouts; }
- const std::vector<graphics::SimpleAnimation *> &SimpleAnimations() const { return simpleAnimations; }
- const std::vector<common::Spell *> &Spells() const { return spells; }
- const std::vector<graphics::Sprite *> &Sprites() const { return sprites; }
- const std::vector<const char *> &Strings() const { return strings; }
- const std::vector<common::TargetingMode *> &TargetingModes() const { return targetingModes; }
- const std::vector<geometry::Vector<int> > &Vectors() const { return vectors; }
+ const std::set<std::string> &ExportedIdentifiers() const { return source.Exports(); }
+ const ParsedDefinition &GetDefinition(const std::string &identifier);
+ const std::map<std::string, SDL_Surface *> &Images() const { return imageCache; }
+ const std::vector<PostponedDefinition> &PostponedDefinitions() const { return postponedDefinitions; }
+ const std::map<int, std::vector<void *> > &Values() const { return values; }
private:
void ReadDefinition(const Definition &);
void ReadLiteral(const Definition &);
void ReadObject(const Definition &);
- graphics::Animation *GetAnimation(const Value &);
- bool GetBoolean(const Value &);
- graphics::Font *GetFont(const Value &);
- graphics::Frame *GetFrame(const Value &);
- graphics::Gauge *GetGauge(const Value &);
- SDL_Surface *GetImage(const Value &);
- int GetNumber(const Value &);
- battle::PartyLayout *GetPartyLayout(const Value &);
- const PropertyList *GetPropertyList(const Value &);
- const std::vector<PropertyList *> &GetPropertyListArray(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<Value *> &GetValueArray(const Value &);
- geometry::Vector<int> 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 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 &);
+ void ReadLiteral(int typeId, int id, char *dest, const Literal &);
+ void *GetObject(int typeId, const Value &value);
+ void ReadObject(int typeId, int id, char *dest, const PropertyList &);
+ void ReadScript(const std::vector<ScriptToken *> &, common::Script *);
+ char *ReadScript(const std::vector<ScriptToken *> &);
+ common::Script::Code &CreateScriptCode(common::Script::Command c, char *dest);
+ void ReadScriptAddress(const ScriptToken &t, char *dest);
+ void ReadScriptInteger(const ScriptToken &t, char *dest);
+ void ReadScriptVector(const ScriptToken &t, char *dest);
+
+ SDL_Surface *GetImage(const std::string &);
+
+ bool CanLink(const Value &) const;
+ void Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType, bool inlined);
private:
const ParsedSource &source;
- enum Type {
- BOOLEAN,
- COMPLEX_ANIMATION,
- FONT,
- FRAME,
- GAUGE,
- HERO,
- IMAGE,
- MONSTER,
- NUMBER,
- PARTY_LAYOUT,
- 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<std::string, ParsedDefinition> parsedDefinitions;
- std::vector<bool> booleans;
- std::vector<graphics::ComplexAnimation *> complexAnimations;
- std::vector<graphics::Font *> fonts;
- std::vector<graphics::Frame *> frames;
- std::vector<graphics::Gauge *> gauges;
- std::vector<battle::Hero *> heroes;
- std::vector<SDL_Surface *> images;
- std::vector<battle::Monster *> monsters;
- std::vector<int> numbers;
- std::vector<battle::PartyLayout *> partyLayouts;
- std::vector<PropertyList *> propertyLists;
- std::vector<std::vector<PropertyList *> > propertyListArrays;
- std::vector<graphics::SimpleAnimation *> simpleAnimations;
- std::vector<common::Spell *> spells;
- std::vector<graphics::Sprite *> sprites;
- std::vector<const char *> strings;
- std::vector<common::TargetingMode *> targetingModes;
- std::vector<std::vector<Value *> > valueArrays;
- std::vector<geometry::Vector<int> > vectors;
+ PagedAllocator alloc;
+
+ std::map<std::string, ParsedDefinition> parsedDefinitions;
+ std::vector<PostponedDefinition> postponedDefinitions;
+ std::map<std::string, SDL_Surface *> imageCache;
+ std::map<int, std::vector<void *> > values;
};