summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-01-16 22:32:56 -0700
committerMike Buland <eichlan@xagasoft.com>2012-01-16 22:32:56 -0700
commit3afa514e11dc16e3a166b39ad411465abd971d8a (patch)
tree3fc9a9b62682c2905909e749f461034634d78903
parent92f50ee62101b67e768b75c9111af3d0258c6ddd (diff)
downloadstage-3afa514e11dc16e3a166b39ad411465abd971d8a.tar.gz
stage-3afa514e11dc16e3a166b39ad411465abd971d8a.tar.bz2
stage-3afa514e11dc16e3a166b39ad411465abd971d8a.tar.xz
stage-3afa514e11dc16e3a166b39ad411465abd971d8a.zip
Lists (arrays) are now properly inedxable.0.02
-rw-r--r--src/gamestate.cpp4
-rw-r--r--src/variable.cpp20
-rw-r--r--src/variable.h7
-rw-r--r--test.stage20
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 )
620 { 620 {
621 throw Bu::ExceptionBase("You cannot use key:value pairs as iterators in a for each loop iterating over a list."); 621 throw Bu::ExceptionBase("You cannot use key:value pairs as iterators in a for each loop iterating over a list.");
622 } 622 }
623 const Variable::VariableList &rList = vIn.getList(); 623 const Variable::VariableArray &rList = vIn.getList();
624 for( Variable::VariableList::const_iterator i = 624 for( Variable::VariableArray::const_iterator i =
625 rList.begin(); i; i++ ) 625 rList.begin(); i; i++ )
626 { 626 {
627 setVariable( vrValue.sName, *i, vrValue.sid ); 627 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
113 return pValue; 113 return pValue;
114} 114}
115 115
116const Variable::VariableList &Variable::getList() const 116const Variable::VariableArray &Variable::getList() const
117{ 117{
118 return *lValue; 118 return *lValue;
119} 119}
@@ -233,9 +233,12 @@ Variable Variable::to( Type e ) const
233 233
234void Variable::insert( const Variable &vKey, const Variable &vValue ) 234void Variable::insert( const Variable &vKey, const Variable &vValue )
235{ 235{
236 if( eType != tDictionary ) 236 if( eType == tDictionary )
237 throw Bu::ExceptionBase("Insert on non-dictionary."); 237 hValue->insert( vKey, vValue );
238 hValue->insert( vKey, vValue ); 238 else if( eType == tList )
239 lValue->get( vKey.getInt() ) = vValue;
240 else
241 throw Bu::ExceptionBase("Insert on non-dictionary and non-list.");
239} 242}
240 243
241bool Variable::has( const Variable &vKey ) 244bool Variable::has( const Variable &vKey )
@@ -244,7 +247,8 @@ bool Variable::has( const Variable &vKey )
244 return hValue->has( vKey ); 247 return hValue->has( vKey );
245 else if( eType == tList ) 248 else if( eType == tList )
246 { 249 {
247 for( VariableList::const_iterator i = lValue->begin(); i; i++ ) 250 for( VariableArray::const_iterator i =
251 const_cast<const VariableArray *>(lValue)->begin(); i; i++ )
248 { 252 {
249 if( (*i) == vKey ) 253 if( (*i) == vKey )
250 return true; 254 return true;
@@ -259,8 +263,10 @@ Variable &Variable::get( const Variable &vKey )
259{ 263{
260 if( eType == tDictionary ) 264 if( eType == tDictionary )
261 return hValue->get( vKey ); 265 return hValue->get( vKey );
266 else if( eType == tList )
267 return lValue->get( vKey.getInt() );
262 else 268 else
263 throw Bu::ExceptionBase("Insert on non-dictionary."); 269 throw Bu::ExceptionBase("Index on non-dictionary and non-list.");
264} 270}
265 271
266Variable &Variable::operator=( const Variable &rhs ) 272Variable &Variable::operator=( const Variable &rhs )
@@ -1006,7 +1012,7 @@ void Variable::initType()
1006 break; 1012 break;
1007 1013
1008 case tList: 1014 case tList:
1009 lValue = new VariableList(); 1015 lValue = new VariableArray();
1010 break; 1016 break;
1011 1017
1012 case tDictionary: 1018 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 @@
5 5
6#include <bu/string.h> 6#include <bu/string.h>
7#include <bu/hash.h> 7#include <bu/hash.h>
8#include <bu/array.h>
8 9
9#include "enums.h" 10#include "enums.h"
10 11
@@ -52,7 +53,7 @@ public:
52 53
53 Type getType() const { return eType; } 54 Type getType() const { return eType; }
54 55
55 typedef Bu::List<Variable> VariableList; 56 typedef Bu::Array<Variable> VariableArray;
56 typedef Bu::Hash<Variable, Variable> VariableHash; 57 typedef Bu::Hash<Variable, Variable> VariableHash;
57 58
58 bool getBool() const; 59 bool getBool() const;
@@ -61,7 +62,7 @@ public:
61 Bu::String getString() const; 62 Bu::String getString() const;
62 VariableRef getVariableRef() const; 63 VariableRef getVariableRef() const;
63 Variable *getVariablePtr() const; 64 Variable *getVariablePtr() const;
64 const VariableList &getList() const; 65 const VariableArray &getList() const;
65 const VariableHash &getHash() const; 66 const VariableHash &getHash() const;
66 67
67 Variable to( Type e ) const; 68 Variable to( Type e ) const;
@@ -101,7 +102,7 @@ private:
101 double fValue; 102 double fValue;
102 bool bValue; 103 bool bValue;
103 Bu::String *sValue; 104 Bu::String *sValue;
104 VariableList *lValue; 105 VariableArray *lValue;
105 VariableHash *hValue; 106 VariableHash *hValue;
106 VariableRef *rValue; 107 VariableRef *rValue;
107 Variable *pValue; 108 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 <<start>>
17{ 17{
18 setup 18 setup
19 { 19 {
20 x = 10; 20 x = [5, 4, 3];
21 display( x ); 21 x = x + [9, 8, 7];
22 x /= 2; 22 display( x[4] );
23 display( x ); 23 x[4] = 112;
24 x *= 4; 24 display ('----');
25 display( x ); 25 for each i in x do
26 x -= 10; 26 {
27 display( x ); 27 display( i );
28 x += 20; 28 }
29 display( x ); 29
30 exit(); 30 exit();
31 } 31 }
32 32