From 3afa514e11dc16e3a166b39ad411465abd971d8a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 16 Jan 2012 22:32:56 -0700 Subject: Lists (arrays) are now properly inedxable. --- src/gamestate.cpp | 4 ++-- src/variable.cpp | 20 +++++++++++++------- src/variable.h | 7 ++++--- test.stage | 20 ++++++++++---------- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 285c151..7777b1f 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -620,8 +620,8 @@ void GameState::parse( const AstBranch::NodeList &lCode ) { throw Bu::ExceptionBase("You cannot use key:value pairs as iterators in a for each loop iterating over a list."); } - const Variable::VariableList &rList = vIn.getList(); - for( Variable::VariableList::const_iterator i = + const Variable::VariableArray &rList = vIn.getList(); + for( Variable::VariableArray::const_iterator i = rList.begin(); i; i++ ) { setVariable( vrValue.sName, *i, vrValue.sid ); diff --git a/src/variable.cpp b/src/variable.cpp index 68a1778..0c83133 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -113,7 +113,7 @@ Variable *Variable::getVariablePtr() const return pValue; } -const Variable::VariableList &Variable::getList() const +const Variable::VariableArray &Variable::getList() const { return *lValue; } @@ -233,9 +233,12 @@ Variable Variable::to( Type e ) const void Variable::insert( const Variable &vKey, const Variable &vValue ) { - if( eType != tDictionary ) - throw Bu::ExceptionBase("Insert on non-dictionary."); - hValue->insert( vKey, vValue ); + if( eType == tDictionary ) + hValue->insert( vKey, vValue ); + else if( eType == tList ) + lValue->get( vKey.getInt() ) = vValue; + else + throw Bu::ExceptionBase("Insert on non-dictionary and non-list."); } bool Variable::has( const Variable &vKey ) @@ -244,7 +247,8 @@ bool Variable::has( const Variable &vKey ) return hValue->has( vKey ); else if( eType == tList ) { - for( VariableList::const_iterator i = lValue->begin(); i; i++ ) + for( VariableArray::const_iterator i = + const_cast(lValue)->begin(); i; i++ ) { if( (*i) == vKey ) return true; @@ -259,8 +263,10 @@ Variable &Variable::get( const Variable &vKey ) { if( eType == tDictionary ) return hValue->get( vKey ); + else if( eType == tList ) + return lValue->get( vKey.getInt() ); else - throw Bu::ExceptionBase("Insert on non-dictionary."); + throw Bu::ExceptionBase("Index on non-dictionary and non-list."); } Variable &Variable::operator=( const Variable &rhs ) @@ -1006,7 +1012,7 @@ void Variable::initType() break; case tList: - lValue = new VariableList(); + lValue = new VariableArray(); break; case tDictionary: diff --git a/src/variable.h b/src/variable.h index 2eedcbb..2e84ae1 100644 --- a/src/variable.h +++ b/src/variable.h @@ -5,6 +5,7 @@ #include #include +#include #include "enums.h" @@ -52,7 +53,7 @@ public: Type getType() const { return eType; } - typedef Bu::List VariableList; + typedef Bu::Array VariableArray; typedef Bu::Hash VariableHash; bool getBool() const; @@ -61,7 +62,7 @@ public: Bu::String getString() const; VariableRef getVariableRef() const; Variable *getVariablePtr() const; - const VariableList &getList() const; + const VariableArray &getList() const; const VariableHash &getHash() const; Variable to( Type e ) const; @@ -101,7 +102,7 @@ private: double fValue; bool bValue; Bu::String *sValue; - VariableList *lValue; + VariableArray *lValue; VariableHash *hValue; VariableRef *rValue; Variable *pValue; diff --git a/test.stage b/test.stage index 654a1e8..2ae34ab 100644 --- a/test.stage +++ b/test.stage @@ -17,16 +17,16 @@ situation <> { setup { - x = 10; - display( x ); - x /= 2; - display( x ); - x *= 4; - display( x ); - x -= 10; - display( x ); - x += 20; - display( x ); + x = [5, 4, 3]; + x = x + [9, 8, 7]; + display( x[4] ); + x[4] = 112; + display ('----'); + for each i in x do + { + display( i ); + } + exit(); } -- cgit v1.2.3