From 107b78b720db69ad402c09c0b1d9beb3b88a1952 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 27 Aug 2012 00:03:28 +0200 Subject: [PATCH] started an interpreter for parsed sources --- Debug/src/loader/subdir.mk | 3 ++ Release/src/loader/subdir.mk | 3 ++ src/loader/Interpreter.cpp | 84 ++++++++++++++++++++++++++++++++++++ src/loader/Interpreter.h | 58 +++++++++++++++++++++++++ src/main.cpp | 5 ++- 5 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 src/loader/Interpreter.cpp create mode 100644 src/loader/Interpreter.h diff --git a/Debug/src/loader/subdir.mk b/Debug/src/loader/subdir.mk index 94e1495..89118df 100644 --- a/Debug/src/loader/subdir.mk +++ b/Debug/src/loader/subdir.mk @@ -4,16 +4,19 @@ # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ +../src/loader/Interpreter.cpp \ ../src/loader/ParsedSource.cpp \ ../src/loader/Parser.cpp \ ../src/loader/Tokenizer.cpp OBJS += \ +./src/loader/Interpreter.o \ ./src/loader/ParsedSource.o \ ./src/loader/Parser.o \ ./src/loader/Tokenizer.o CPP_DEPS += \ +./src/loader/Interpreter.d \ ./src/loader/ParsedSource.d \ ./src/loader/Parser.d \ ./src/loader/Tokenizer.d diff --git a/Release/src/loader/subdir.mk b/Release/src/loader/subdir.mk index c865204..50f5baf 100644 --- a/Release/src/loader/subdir.mk +++ b/Release/src/loader/subdir.mk @@ -4,16 +4,19 @@ # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ +../src/loader/Interpreter.cpp \ ../src/loader/ParsedSource.cpp \ ../src/loader/Parser.cpp \ ../src/loader/Tokenizer.cpp OBJS += \ +./src/loader/Interpreter.o \ ./src/loader/ParsedSource.o \ ./src/loader/Parser.o \ ./src/loader/Tokenizer.o CPP_DEPS += \ +./src/loader/Interpreter.d \ ./src/loader/ParsedSource.d \ ./src/loader/Parser.d \ ./src/loader/Tokenizer.d diff --git a/src/loader/Interpreter.cpp b/src/loader/Interpreter.cpp new file mode 100644 index 0000000..268fab9 --- /dev/null +++ b/src/loader/Interpreter.cpp @@ -0,0 +1,84 @@ +/* + * Interpreter.cpp + * + * Created on: Aug 26, 2012 + * Author: holy + */ + +#include "Interpreter.h" + +#include "ParsedSource.h" +#include "../battle/Monster.h" + +using battle::Monster; +using std::map; +using std::set; +using std::string; + +namespace loader { + +void Interpreter::ReadSource() { + for (set::const_iterator i(source.Exports().begin()), end(source.Exports().end()); i != end; ++i) { + const Definition &dfn(source.GetDefinition(*i)); + if (dfn.HasLiteralValue()) { + ReadLiteral(dfn); + } else { + ReadObject(dfn); + } + } +} + +void Interpreter::ReadLiteral(const Definition &dfn) { + switch (dfn.GetLiteral()->GetType()) { + case Literal::ARRAY_VALUES: + throw Error("unhandled literal: array of values"); + break; + case Literal::ARRAY_PROPS: + throw Error("unhandled literal: array of values"); + break; + case Literal::BOOLEAN: + throw Error("unhandled literal: array of values"); + break; + case Literal::COLOR: + throw Error("unhandled literal: array of values"); + break; + case Literal::NUMBER: + throw Error("unhandled literal: array of values"); + break; + case Literal::STRING: + throw Error("unhandled literal: array of values"); + break; + case Literal::VECTOR: + throw Error("unhandled literal: array of values"); + break; + case Literal::OBJECT: + throw Error("unhandled literal: array of values"); + break; + } +} + +void Interpreter::ReadObject(const Definition &dfn) { + if (dfn.TypeName() == "Monster") { + monsters.resize(monsters.size() + 1); + ReadMonster(monsters.back(), *dfn.GetProperties()); + } else { + throw Error("unhandled object: " + dfn.TypeName()); + } +} + + +void Interpreter::ReadMonster(Monster &m, const PropertyList &props) { + for (PropertyList::ConstIterator i(props.Begin()), end(props.End()); i != end; ++i) { + if (i->first == "name") { + if (i->second->IsLiteral()) { + m.SetName(i->second->GetLiteral().GetString().c_str()); + } else { + throw Error("identifier resolution not implemented"); + } + } else { + throw Error("unknown monster property: " + i->first); + } + } +} + +} diff --git a/src/loader/Interpreter.h b/src/loader/Interpreter.h new file mode 100644 index 0000000..c2fc25f --- /dev/null +++ b/src/loader/Interpreter.h @@ -0,0 +1,58 @@ +/* + * Interpreter.h + * + * Created on: Aug 26, 2012 + * Author: holy + */ + +#ifndef LOADER_INTERPRETER_H_ +#define LOADER_INTERPRETER_H_ + +#include +#include +#include +#include + +namespace battle { + class Monster; +} + +namespace loader { + +class Definition; +class ParsedSource; +class PropertyList; + +class Interpreter { + +public: + 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) { } + ~Interpreter() { } +private: + Interpreter(const Interpreter &); + Interpreter &operator =(const Interpreter &); + +public: + void ReadSource(); + +private: + void ReadLiteral(const Definition &); + void ReadObject(const Definition &); + + void ReadMonster(battle::Monster &, const PropertyList &); + +private: + const ParsedSource &source; + std::vector monsters; + +}; + +} + +#endif /* LOADER_INTERPRETER_H_ */ diff --git a/src/main.cpp b/src/main.cpp index ddd6b7a..7a8b425 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,7 @@ #include "graphics/Menu.h" #include "graphics/SimpleAnimation.h" #include "graphics/Sprite.h" +#include "loader/Interpreter.h" #include "loader/ParsedSource.h" #include "loader/Parser.h" #include "sdl/InitImage.h" @@ -57,6 +58,7 @@ using graphics::Gauge; using graphics::Menu; using graphics::SimpleAnimation; using graphics::Sprite; +using loader::Interpreter; using loader::ParsedSource; using loader::Parser; using sdl::InitImage; @@ -80,8 +82,9 @@ int main(int argc, char **argv) { ParsedSource source; Parser parser("test-data/test.l2s", source); parser.Parse(); - cout << source; + Interpreter intp(source); + intp.ReadSource(); return 0; -- 2.39.2