]> git.localhorst.tv Git - l2e.git/commitdiff
added script labels in parser/interpreter
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 14 Oct 2012 21:19:58 +0000 (23:19 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 14 Oct 2012 21:25:17 +0000 (23:25 +0200)
src/loader/Interpreter.cpp
src/loader/ParsedSource.cpp
src/loader/ParsedSource.h
src/loader/Parser.cpp

index 0fd2d883fba0a8129b3a896dd56d37f35e4af96f..bd7473aebab2a8673d4f419adbdc359efaa59e8b 100644 (file)
@@ -343,9 +343,16 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis
 
 
 void Interpreter::ReadScript(const std::vector<ScriptToken *> &s, Script *script) {
+       std::map<string, int> labels;
        int size(0);
        for (vector<ScriptToken *>::const_iterator i(s.begin()), end(s.end()); i != end; ++i) {
-               if ((*i)->GetType() != ScriptToken::COMMAND) {
+               if ((*i)->GetType() == ScriptToken::LABEL) {
+                       if (labels.count((*i)->Label())) {
+                               throw Error("duplicate label " + (*i)->Label());
+                       } else {
+                               labels[(*i)->Label()] = size;
+                       }
+               } else if ((*i)->GetType() != ScriptToken::COMMAND) {
                        throw Error("unexpected script token");
                }
                ++size;
@@ -423,6 +430,9 @@ void Interpreter::ReadScript(const std::vector<ScriptToken *> &s, Script *script
        unsigned char *text(reinterpret_cast<unsigned char *>(alloc.Alloc(size)));
        int cursor(0);
        for (vector<ScriptToken *>::const_iterator i(s.begin()), end(s.end()); i != end; ++i) {
+               if ((*i)->GetType() == ScriptToken::LABEL) {
+                       continue;
+               }
                if ((*i)->GetType() != ScriptToken::COMMAND) {
                        throw Error("unexpected script token");
                }
@@ -544,9 +554,9 @@ void Interpreter::ReadScriptAddress(const ScriptToken &t, unsigned char *dest) {
        if (t.GetType() != ScriptToken::IDENTIFIER) {
                throw Error("expected identifier for address");
        }
-       if (source.IsDefined(t.GetIdentifier())) {
-               const ParsedDefinition &def(GetDefinition(t.GetIdentifier()));
-               void *addr(GetObject(def.type, t.GetIdentifier()));
+       if (source.IsDefined(t.Identifier())) {
+               const ParsedDefinition &def(GetDefinition(t.Identifier()));
+               void *addr(GetObject(def.type, t.Identifier()));
                *reinterpret_cast<void **>(dest) = addr;
        } else {
                throw Error("postponing values in scripts not implemented");
@@ -555,8 +565,8 @@ void Interpreter::ReadScriptAddress(const ScriptToken &t, unsigned char *dest) {
 
 void Interpreter::ReadScriptInteger(const ScriptToken &t, unsigned char *dest) {
        if (t.GetType() == ScriptToken::IDENTIFIER) {
-               if (source.IsDefined(t.GetIdentifier())) {
-                       void *num(GetObject(TypeDescription::GetTypeId("Number"), t.GetIdentifier()));
+               if (source.IsDefined(t.Identifier())) {
+                       void *num(GetObject(TypeDescription::GetTypeId("Number"), t.Identifier()));
                        *reinterpret_cast<int *>(dest) = *reinterpret_cast<int *>(num);
                } else {
                        throw Error("postponing values in scripts not implemented");
@@ -570,8 +580,8 @@ void Interpreter::ReadScriptInteger(const ScriptToken &t, unsigned char *dest) {
 
 void Interpreter::ReadScriptVector(const ScriptToken &t, unsigned char *dest) {
        if (t.GetType() == ScriptToken::IDENTIFIER) {
-               if (source.IsDefined(t.GetIdentifier())) {
-                       void *vec(GetObject(TypeDescription::GetTypeId("Vector"), t.GetIdentifier()));
+               if (source.IsDefined(t.Identifier())) {
+                       void *vec(GetObject(TypeDescription::GetTypeId("Vector"), t.Identifier()));
                        *reinterpret_cast<Vector<int> *>(dest) = *reinterpret_cast<Vector<int> *>(vec);
                } else {
                        throw Error("postponing values in scripts not implemented");
index 6b2fc80413207811b90631c1bfc493982c95c5ee..8c3d3bd17415d5cd4fe7ff92df6b42a45204127a 100644 (file)
@@ -485,7 +485,7 @@ const string &ScriptToken::CommandName() const {
        }
 }
 
-const string &ScriptToken::GetIdentifier() const {
+const string &ScriptToken::Identifier() const {
        if (type == IDENTIFIER) {
                return str;
        } else {
@@ -493,6 +493,14 @@ const string &ScriptToken::GetIdentifier() const {
        }
 }
 
+const string &ScriptToken::Label() const {
+       if (type == LABEL) {
+               return str;
+       } else {
+               throw runtime_error("access to label of non-label script token");
+       }
+}
+
 const Literal *ScriptToken::GetLiteral() const {
        if (type == LITERAL) {
                return literal;
index 9ba52972a4b5bcf797fc32fc62d80db9f183553e..6a7a2d4eed684fcaca5e42d01e40eaca97f02113 100644 (file)
@@ -26,6 +26,7 @@ public:
                REGISTER,
                IDENTIFIER,
                LITERAL,
+               LABEL,
        };
 
        ScriptToken(const std::string &, Type);
@@ -39,7 +40,8 @@ public:
        Type GetType() const { return type; }
        const std::string &RegisterName() const;
        const std::string &CommandName() const;
-       const std::string &GetIdentifier() const;
+       const std::string &Identifier() const;
+       const std::string &Label() const;
        const Literal *GetLiteral() const;
 
 private:
index 9ddae34c5bdd4b2ac2a688c6765c925c05013917..9641fadfae828db61439dee05683a017e71afba9 100644 (file)
@@ -371,7 +371,13 @@ Literal *Parser::ParseScript() {
                                                break;
                                        }
                                        case Token::IDENTIFIER: {
-                                               script.push_back(new ScriptToken(t.str, ScriptToken::IDENTIFIER));
+                                               Token t2(GetToken());
+                                               if (t2.type == Token::COLON) {
+                                                       script.push_back(new ScriptToken(t.str, ScriptToken::LABEL));
+                                               } else {
+                                                       tok.Putback(t2);
+                                                       script.push_back(new ScriptToken(t.str, ScriptToken::IDENTIFIER));
+                                               }
                                                break;
                                        }
                                        case Token::REGISTER: {