]> git.localhorst.tv Git - l2e.git/commitdiff
started an interpreter for parsed sources
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 26 Aug 2012 22:03:28 +0000 (00:03 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 26 Aug 2012 22:03:28 +0000 (00:03 +0200)
Debug/src/loader/subdir.mk
Release/src/loader/subdir.mk
src/loader/Interpreter.cpp [new file with mode: 0644]
src/loader/Interpreter.h [new file with mode: 0644]
src/main.cpp

index 94e14953c96a9535236281b828ce49f33128522f..89118dfbf9fa6e2c1f8ea4b884cadf5fadbced24 100644 (file)
@@ -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 
index c865204ca1f7829ace3d2470056324892c1ccedb..50f5baf397d36ca13e511c1af0dc378cad538719 100644 (file)
@@ -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 (file)
index 0000000..268fab9
--- /dev/null
@@ -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<string>::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 (file)
index 0000000..c2fc25f
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Interpreter.h
+ *
+ *  Created on: Aug 26, 2012
+ *      Author: holy
+ */
+
+#ifndef LOADER_INTERPRETER_H_
+#define LOADER_INTERPRETER_H_
+
+#include <map>
+#include <stdexcept>
+#include <string>
+#include <vector>
+
+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<battle::Monster> monsters;
+
+};
+
+}
+
+#endif /* LOADER_INTERPRETER_H_ */
index ddd6b7adcda7cefc261bbb074316eccc1334d6ab..7a8b425d1a8bea52ddb3f302556f8c1ca5923d3e 100644 (file)
@@ -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;